2012-09-22 54 views
0

我有下面的查詢變得更加有用:的Oracle SQL - 當一個案例查詢比運行兩個獨立的querys

--Total DLs-- 
select sum(DLs) as DLs_Total 
From 
(
Select 
Count(T.Create_Dtime) As Dls 
From Player_Tapjoy T 
Inner Join Player P On T.Player_Id=P.Player_Id 
Where P.ReferredBy IS NULL 
Union All 
Select 
Count(Pt.Create_Dtime) As DLs 
From Player_Aux_Pt Pt 
Inner Join Player P On Pt.Player_Id=P.Player_Id 
Where 
Pt.Site = 'AppCircle' 
And P.ReferredBy IS NULL 
) 

我將這份報告運行兩次,一次爲「而且P.ReferredBy是空」,並曾經爲‘而且P.ReferredBy不爲空’。

我覺得這可能是切換到區分查詢......所以如果null或不爲空,一個查詢的情況下,會產生兩個不同的結果的好時機?

回答

1

是的,你可以在這裏使用情況。我還沒有測試過下面的查詢,但請檢查看看它是否有效:

--Total DLs-- 
select sum(DLs_ReferredBy_Null) as DLs_ReferredBy_Null_Total, sum(DLs_ReferredBy_NotNull) as DLs_DLs_ReferredBy_NotNull_Total 
From 
(
Select 
Count(CASE WHEN P.ReferredBy IS NULL THEN T.Create_Dtime END) As DLs_ReferredBy_Null, 
Count(CASE WHEN P.ReferredBy IS NOT NULL THEN T.Create_Dtime END) As DLs_ReferredBy_NotNull 
From Player_Tapjoy T 
Inner Join Player P On T.Player_Id=P.Player_Id 
Union All 
Select 
Count(CASE WHEN P.ReferredBy IS NULL THEN Pt.Create_Dtime END) As DLs_ReferredBy_Null, 
Count(CASE WHEN P.ReferredBy IS NOT NULL THEN Pt.Create_Dtime END) As DLs_ReferredBy_NotNull 
From Player_Aux_Pt Pt 
Inner Join Player P On Pt.Player_Id=P.Player_Id 
Where 
Pt.Site = 'AppCircle' 
) 
+1

那些ELSE 0應該是ELSE NULL,因爲COUNT也會計數爲零。 – GuiGi

+0

嗨@GuiGi!是的,你是完全正確的。此外,在這種情況下,否則甚至不會需要因爲這將是隱含的。只是在上面更改了它。謝謝。 – RGO