Oracle SQL - 按日期加入多个查询

时间:2012-09-27 17:55:39

标签: sql oracle

我在下面有一个大型查询,我按日期加入了五个查询:

--Combined Daily Actions Per Revenue Source-- 
SELECT Two.the_date, 
       sp_dau, 
       cs_dau, 
       tapjoy_ios_dau, 
       tapjoy_android_dau, 
       appcircle_ios_dau, 
       appcircle_android_dau, 
       freecause_dau, 
       portal_dau 
FROM   (SELECT Trunc(Cs.create_dtime) AS The_Date, 
               Count(DISTINCT CASE 
                                WHEN Cs.cs_listing_id LIKE '99999999%' THEN 
                                ( Cs.player_id ) 
                              END)    AS Sp_Dau, 
               Count(DISTINCT CASE 
                                WHEN Cs.cs_listing_id NOT LIKE '99999999%' THEN 
                                ( Cs.player_id ) 
                              END)    AS Cs_Dau 
        FROM   player_chkin_cs Cs 
        WHERE  Trunc(Cs.create_dtime) >= To_date('2012-Jan-01', 'yyyy-mon-dd') 
        GROUP  BY Trunc(Cs.create_dtime)) One 
       INNER JOIN (SELECT Trunc(Tap.create_dtime) AS The_Date, 
                          Count(DISTINCT CASE 
                                           WHEN ( Play.uuid LIKE 'i~%' ) 
                                                 OR ( Play.uuid LIKE 'ti~%' ) 
                                         THEN 
                                           Tap.player_id 
                                         END)     AS Tapjoy_Ios_Dau, 
                          Count(DISTINCT CASE 
                                           WHEN ( Play.uuid LIKE 'a~%' ) 
                                                 OR ( Play.uuid LIKE 'ta~%' ) 
                                         THEN 
                                           Tap.player_id 
                                         END)     AS Tapjoy_Android_DAU 
                   FROM   player_tapjoy Tap 
                          INNER JOIN player Play 
                                  ON Tap.player_id = Play.player_id 
                   WHERE  Trunc(Tap.create_dtime) >= 
                          To_date('2012-Jan-01', 'yyyy-mon-dd') 
                   GROUP  BY Trunc(Tap.create_dtime)) Two 
               ON One.the_date = Two.the_date 
       INNER JOIN (SELECT Trunc(Aux.create_dtime) AS The_Date, 
                          Count(DISTINCT CASE 
                                           WHEN ( Play.uuid LIKE 'i~%' ) 
                                                 OR ( Play.uuid LIKE 'ti~%' ) 
                                         THEN 
                                           Aux.player_id 
                                         END)     AS Appcircle_Ios_Dau, 
                          Count(DISTINCT CASE 
                                           WHEN ( Play.uuid LIKE 'a~%' ) 
                                                 OR ( Play.uuid LIKE 'ta~%' ) 
                                         THEN 
                                           Aux.player_id 
                                         END)     AS AppCircle_Android_DAU 
                   FROM   player_aux_pt Aux 
                          INNER JOIN player Play 
                                  ON Aux.player_id = Play.player_id 
                   WHERE  Aux.site = 'AppCircle' 
                          AND Trunc(Aux.create_dtime) >= 
                              To_date('2012-Jan-01', 'yyyy-mon-dd') 
                   GROUP  BY Trunc(Aux.create_dtime))Three 
               ON Two.the_date = Three.the_date 
       INNER JOIN (SELECT Trunc(Aux.create_dtime)       AS The_Date, 
                          Count(DISTINCT Aux.player_id) AS FreeCause_DAU 
                   FROM   player_aux_pt Aux 
                   WHERE  Aux.site = 'ext : freecause' 
                          AND Trunc(Aux.create_dtime) >= 
                              To_date('2012-Jan-01', 'yyyy-mon-dd') 
                   GROUP  BY Trunc(Aux.create_dtime))Four 
               ON Three.the_date = Four.the_date 
       INNER JOIN (SELECT Trunc(Aux.create_dtime)       AS The_Date, 
                          Count(DISTINCT Aux.player_id) AS Portal_DAU 
                   FROM   player_aux_pt Aux 
                   WHERE  ( Aux.site = 'Portal : Promotion' 
                             OR Aux.site = 'Portal : RadiumOne' 
                             OR Aux.site = 'Portal : Paymentwall' 
                             OR Aux.site = 'Portal : TrialPay' ) 
                          AND Trunc(Aux.create_dtime) >= 
                              To_date('2012-Jan-01', 'yyyy-mon-dd') 
                   GROUP  BY Trunc(Aux.create_dtime)) Five 
               ON Four.the_date = Five.the_date 

大多数子查询的范围从2012-Jan-01到当前日期,除了只有09-Jul-12的数据存在的子查询。

因此,当我运行此查询时,结果中的第一个日期为09-Jul-12而不是01-Jan-12

如何才能从Jan 01开始获取结果,其中除了一个查询之外的所有查询都包含相关数据?

1 个答案:

答案 0 :(得分:2)

您的问题是日期因为不匹配而退出。您的问题的答案是LEFT OUTER JOIN,而不是INNER JOIN。这将保留第一个表中的所有行(连接的左侧)以及下一个表中的任何匹配信息。如果没有匹配,则所有值都变为NULL。

假设第一个表包含您想要的所有日期,请在所有后续查询中更改连接。

如果你想要0的instaed为NULL,那么在coalesce()子句中使用select来转换它们。