[英]Oracle SQL - Joining Multiple Queries By the Date
我在下面有這個大型查詢,我按日期加入了五個查詢:
--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
日開始得到結果,其中除了一個查詢之外的所有查詢都有相關的數據?
您的問題是日期因為不匹配而退出。 您的問題的答案是LEFT OUTER JOIN
而不是INNER JOIN
。 這將保留第一個表中的所有行(連接的左側)以及下一個表中的任何匹配信息。 如果沒有匹配,則所有值都變為NULL。
假設第一個表具有您想要的所有日期,則在所有后續查詢中更改連接。
如果你想要0的instaed為NULL,那么在select
子句中使用coalesce()
來轉換它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.