简体   繁体   English

使用子查询作为字段列

[英]Using SubQuery as Field Column

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.如您所见,我想使用 unit_profit、customer_rate 和 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.您需要做的是获取所有在苍蝇内部完成的子查询,并创建与 CDR 表的连接。

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.您现在所做的是对 CDR 中的每个记录执行 3 个查询。 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.如果这个表(CDR)只有几条记录是可以的,但如果没有,这可能会消耗大量的处理器、内存和磁盘 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.执行“加入”并以相同格式显示信息的技巧是加入 3 次相同的子查询,但使用不同的别名。

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 @leo

Correlated subqueries like you have in your query generally suck when it comes to performance.在性能方面,像您在查询中拥有的相关子查询通常很糟糕。 Since you only retrieve 100 rows, it shouldn't be too bad, but if you want it faster you have to rewrite your query.由于您只检索 100 行,因此应该不会太糟糕,但如果您希望它更快,则必须重写您的查询。

The problem at hand can be fixed easily with:手头的问题可以通过以下方式轻松解决:

SELECT *, unit_profit * duration AS my_calc
FROM (
   -- your query here
   -- just without "unit_profit * duration"
   -- and maybe without redundant column aliases
   ) AS sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM