0

我想使用嵌套 SELECT 将 code_2 的结果转换为 code_1 的结果。你能帮我做这件事吗?我使用数据库 - Oracle PL/SQL Developer 提前非常感谢

代码_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 

代码_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) 
4

1 回答 1

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 
于 2013-05-30T17:49:48.113 回答