如何做嵌套SELECT

时间:2013-05-30 17:45:37

标签: sql oracle select nested

我希望使用嵌套的SELECT将code_2的结果看作code_1的结果。 你能帮帮我吗? 我使用数据库 - Oracle PL / SQL Developer 非常感谢提前

code_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:

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) 

1 个答案:

答案 0 :(得分:0)

要将此作为嵌套选择,您只需将第二个查询添加到第一个查询的选择行。它返回一个值和一行,所以没关系。从概念上讲,这是:

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 

distinct是不必要的。)

在实践中,这看起来有点糟糕:

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 

另一种方法是使用分析函数,因为此查询不需要嵌套选择。

在回复您的评论时,一种方法是相关子查询:

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;

更简单的方法可能是将其直接移动到聚合中。 。但您可能需要使用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