2012-09-26 64 views
2

內的值我在Oracle SQL下面的查詢:甲骨文SQL-獲得「另類」一個「CASE」查詢

Select 
Trunc(Cs.Create_Dtime), 
Count(Case When Cs.Cs_Listing_Id Like '99999999%' Then (Cs.Player_Id) End) As Sp_Dau, 
Count(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) 
Order By 1 ASC 

我加入之前「案例」每個計數「另類」。我只是想確保這隻會在每種情況下返回所有不同的player_Ids。有人可以證實嗎?謝謝!下面是最終的查詢:

Select 
Trunc(Cs.Create_Dtime), 
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) 
Order By 1 ASC; 
+1

應該很容易通過檢查輸出爲自己確定,但肯定的,它應該用'Cs_Listing_id LIKE'99999999%''返回不同Player_Id的計數。 –

+0

Mike-謝謝 - 由於數據太多,我無法確認輸出。但欣賞的迴應。 – Americo

+1

@Stuav - 如果「數據太多」,這意味着您需要使用數據較少的上下文(比如數據庫的本地副本),只需確定「預期」行爲所需的相關行即可。 –

回答

1

一個簡單的測試案例爲您證明count(distinct ...只返回不同的值:

11:34:09 [email protected]_xe> select department_id, count(*) from employees group by department_id order by 2 desc;  

DEPARTMENT_ID COUNT(*)                      
------------- ----------                      
      50   45                      
      80   34                      
      100   6                      
      30   6                      
      60   5                      
      90   3                      
      20   2                      
      110   2                      
      40   1                      
      10   1                      
         1                      
      70   1                      

12 rows selected.                        

Elapsed: 00:00:00.03                       
11:34:12 [email protected]_xe> select count(department_id) "ALL", count(distinct department_id) "DISTINCT" from employees; 

     ALL DISTINCT                       
---------- ----------                       
     106   11                       

1 row selected.                        

Elapsed: 00:00:00.02                       
11:34:20 [email protected]_xe>