I have the query which has subquery as field column but i want to use this field column in other place.
SELECT c.country_code AS
country_code,
c.dial_code AS
dial_code,
(SELECT r.destination
FROM region r
WHERE r.country_code = c.country_code
AND r.dial_code = c.dial_code) AS
destination,
c.start_time,
c.duration,
c.call_type,
c.customer_prefix AS
customer_prefix,
c.vendor_prefix AS
vendor_prefix,
(SELECT Round(r.rate, 3)
FROM rate r
INNER JOIN region re
ON r.region_id = re.id
INNER JOIN account_prefix ap
ON r.account_prefix_id = ap.id
WHERE re.country_code = c.country_code
AND re.dial_code = c.dial_code
AND ap.prefix = c.customer_prefix
AND ap.prefix_type = 0) AS
**customer_rate**,
(SELECT Round(r.rate, 3)
FROM rate r
INNER JOIN region re
ON r.region_id = re.id
INNER JOIN account_prefix ap
ON r.account_prefix_id = ap.id
WHERE re.country_code = c.country_code
AND re.dial_code = c.dial_code
AND ap.prefix = c.vendor_prefix
AND ap.prefix_type = 1) AS
**vendor_rate**,
(SELECT Round(r.rate, 3)
FROM rate r
INNER JOIN region re
ON r.region_id = re.id
INNER JOIN account_prefix ap
ON r.account_prefix_id = ap.id
WHERE re.country_code = c.country_code
AND re.dial_code = c.dial_code
AND ap.prefix = c.customer_prefix
AND ap.prefix_type = 0) - (SELECT Round(r.rate, 3)
FROM rate r
INNER JOIN region re
ON r.region_id = re.id
INNER JOIN account_prefix ap
ON r.account_prefix_id
= ap.id
WHERE
re.country_code = c.country_code
AND re.dial_code = c.dial_code
AND ap.prefix = c.vendor_prefix
AND ap.prefix_type = 1) AS **unit_profit**,
unit_profit * duration
FROM cdr c
LIMIT 100;
As you can see, I want to use the unit_profit, customer_rate, and vendor_rate. How to achieve it ?
EDIT:
Any tutorial that showing join on view ?
What you need to do is to take all those subqueries done inside the flieds, and create a join with the CDR table.
This will greatly improve the performance of the query and response time. What you are doing now is executing 3 queries for each for the records at CDR. Ff this table (CDR) have just a few records is fine, but if not this could consume lot of processor, memory and disk I/O.
The trick to do the "join" and show the info in the same format is to join 3 times the same subquery but with a different alias.
select c.country_code, customer_rate_table.customer_rate
from CDR c
left outer join on ( SELECT Round(r.rate, 3) customer_rate , re.country_code,
re.dial_code, re.dial_code, ap.prefix
FROM rate r
INNER JOIN region re
ON r.region_id = re.id
INNER JOIN account_prefix ap
ON r.account_prefix_id = ap.id
WHERE ap.prefix_type = 1
) customer_rate_table
ON customer_rate.country_code = c.country_code
AND customer_rate.dial_code = c.dial_code
AND customer_rate.prefix = c. customer_prefix
left outer join on ( {Same as above but with the right fields} ) vendor_rate_table
ON vendor_rate_table.country_code = c.country_code
AND vendor_rate_table.dial_code = c.dial_code
AND vendor_rate_table.prefix = c.vendor_prefix
and then the next table...
This code is not complete but I think gives an explanation on what you need to do.
Thanks!
@leo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With