簡體   English   中英

Oracle SQL-在“CASE”查詢中獲取“Distinct”值

[英]Oracle SQL- Getting “Distinct” values within a “CASE” query

我在ORACLE SQL中有以下查詢:

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

我在每個計數的“案例”之前添加了“Distinct”。 我只想確保在每種情況下只返回所有不同的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;

一個簡單的測試用例,供您證明count(distinct ...僅返回不同的值:

11:34:09 HR@vm_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 HR@vm_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 HR@vm_xe>                                                                                            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM