案例陈述中的不同条件

时间:2012-11-06 19:31:11

标签: sql oracle

我有一个SQL,它为每个Event_ID返回一个cnt1和cnt2,同时测试列“POSA”的条件

根据需要返回以下内容:

Event_ID | CNT1 | CNT2

我想为每个CASE语句添加另一个条件,该语句测试同一个表中的另一列“S”是否不同。以下是代码段:

SELECT Event_ID, 
 sum  
 (CASE WHEN POSA IN ('SX', 'DX')  THEN 1 
    ELSE 0 
 END) 
 AS cnt1,
sum 
(CASE  
    WHEN POSA IN ('SP', 'DP')  THEN 1 
    ELSE 0 
 END) AS cnt2


FROM Station_Processed_Info GROUP BY EVENT_ID;

1 个答案:

答案 0 :(得分:1)

SELECT Event_ID,   
SUM(CASE 
   WHEN POSA IN ('SX', 'DX')  THEN 1
   WHEN DISTINCT("S") THEN something 
    ELSE 0   END)   AS cnt1, 
SUM (CASE  
    WHEN POSA IN ('SP', 'DP')  THEN 1
    WHEN DISTINCT("S") THEN something 
    ELSE 0   END) AS cnt2    
FROM Station_Processed_Info GROUP BY EVENT_ID;