简体   繁体   English

如何做嵌套的选择

[英]How to do Nested SELECT

I would like to see the result of code_2 into result ofcode_1 using nested SELECT. 我想使用嵌套SELECT将code_2的结果转换为code_1的结果。 Can you help me to do this? 你能帮我做到吗? I use database - Oracle PL/SQL Developer Thanks a lot in advance 我使用数据库-Oracle PL / SQL Developer非常感谢

code_1: 代码_1:

SELECT DISTINCT eventattribute1
FROM   table1 partition(m201302) 
WHERE  eventattribute22 = 'EURO' 
       AND eventattribute13 = 'MC_PO_ROAM' 
       AND eventattribute14 != 'T' 
       AND event_cost != '0' 
GROUP  BY eventattribute1 

code_2: 代码_2:

SELECT Numtodsinterval(Sum(To_char(To_date(duration, 'HH24:MI:SS'), 
                           'HH24') * 
                                  3600 + 
                                                      To_char( 
                                  To_date(duration, 'HH24:MI:SS'), 
                                                      'MI') 
                                                      * 60 + 
                                  To_char(To_date(duration, 
                                          'HH24:MI:SS'), 'SS')), 'second') AS 
       SUMTOTAL 
FROM   table1 partition(m201302) 

To do this as a nested select, you can just add the second query onto the select line of the first. 要将其作为嵌套选择,只需将第二个查询添加到第一个查询的选择行即可。 It returns one value and one row, so that is fine. 它返回一个值和一行,所以很好。 Conceptually, this is: 从概念上讲,这是:

SELECT eventattribute1,
       <code2>
FROM   table1 partition(m201302) 
WHERE  eventattribute22 = 'EURO' 
       AND eventattribute13 = 'MC_PO_ROAM' 
       AND eventattribute14 != 'T' 
       AND event_cost != '0' 
GROUP  BY eventattribute1 

(The distinct is unnecessary.) distinct是不必要的。)

In practice, this looks a bit worse: 实际上,这看起来更糟:

SELECT eventattribute1,
       (SELECT Numtodsinterval(Sum(To_char(To_date(duration, 'HH24:MI:SS'), 
                       'HH24') * 
                              3600 + 
                                                  To_char( 
                              To_date(duration, 'HH24:MI:SS'), 
                                                  'MI') 
                                                  * 60 + 
                              To_char(To_date(duration, 
                                      'HH24:MI:SS'), 'SS')), 'second') AS SUMTOTAL 
        FROM   table1 partition(m201302)
       ) as SUMTOTAL
FROM   table1 partition(m201302) 
WHERE  eventattribute22 = 'EURO' 
       AND eventattribute13 = 'MC_PO_ROAM' 
       AND eventattribute14 != 'T' 
       AND event_cost != '0' 
GROUP  BY eventattribute1 

An alternative is to use an analytic function, because you don't need a nested select for this query. 一种替代方法是使用解析函数,因为您不需要此查询的嵌套选择。

In response to your comment, one method is a correlated subquery: 为了回应您的评论,一种方法是相关子查询:

SELECT eventattribute1,
       (SELECT Numtodsinterval(Sum(To_char(To_date(duration, 'HH24:MI:SS'), 
                       'HH24') * 
                              3600 + 
                                                  To_char( 
                              To_date(duration, 'HH24:MI:SS'), 
                                                  'MI') 
                                                  * 60 + 
                              To_char(To_date(duration, 
                                      'HH24:MI:SS'), 'SS')), 'second') AS SUMTOTAL 
        FROM   table1 partition(m201302) t1
        where t1.eventattribute1 = table1.eventattribute1
       ) as SUMTOTAL
FROM   table1 partition(m201302) 
WHERE  eventattribute22 = 'EURO' 
       AND eventattribute13 = 'MC_PO_ROAM' 
       AND eventattribute14 != 'T' 
       AND event_cost != '0' 
GROUP  BY eventattribute1;

The easier way might be to move it directly into the aggregation . 更简单的方法可能是将其直接移到聚合中。 . but you might need to play with the conditions in the where clause; 但是您可能需要使用where子句中的条件;

SELECT eventattribute1,
       Numtodsinterval(Sum(To_char(To_date(duration, 'HH24:MI:SS'), 
                       'HH24') * 
                              3600 + 
                                                  To_char( 
                              To_date(duration, 'HH24:MI:SS'), 
                                                  'MI') 
                                                  * 60 + 
                              To_char(To_date(duration, 
                                      'HH24:MI:SS'), 'SS')), 'second') AS SUMTOTAL 
FROM   table1 partition(m201302) 
WHERE  eventattribute22 = 'EURO' 
       AND eventattribute13 = 'MC_PO_ROAM' 
       AND eventattribute14 != 'T' 
       AND event_cost != '0' 
GROUP  BY eventattribute1 

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

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