sql - Oracle SQL-在 "Distinct"查询中获取 "CASE"值

标签 sql oracle

我在 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

我在每个计数的“case”之前添加了“Distinct”。我只是想确保在每种情况下这只会返回所有不同的 player_Id。有人可以确认吗?谢谢!这是最终查询:

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>                                                                                            

关于sql - Oracle SQL-在 "Distinct"查询中获取 "CASE"值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593477/

相关文章:

sql - 如何在sql中比较小时和分钟

java - ORA-02289 : sequence does not exist when upgrade Hibernate 3 to hibernate 4

oracle - Oracle中触发器的BEGIN语句后可以声明CURSOR吗?

oracle - 第一条记录上的 SQL 加载程序错误

mysql - 如何使用 group by 子句从单个表中检索数据

sql - 从表列中的 SQL Server 日期值获取月份名称

使用 Oracle 11g 的 Oracle 开发人员 VM

sql - 通过 Oracle SQL 检索范围内的数字

mysql - 从与另一个表关联的表中删除重复项

java - 对 JPA 连接的 SQL 查询