Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL- Getting "Distinct" values within a "CASE" query

I have the following query in 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

I added "Distinct" just before "case" for each count. I just want to make sure that this only returns all of the distinct player_Ids in each case. Can some one confirm? Thank you! Here is the final query:

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;
like image 911
Americo Avatar asked Jul 31 '26 22:07

Americo


1 Answers

A simple test case for you to prove count(distinct ... returns only distinct values:

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>                                                                                            
like image 53
Kirill Leontev Avatar answered Aug 02 '26 16:08

Kirill Leontev