2016-03-08 54 views
0

如何在我的「case expression」中使用IN在「case」表達式中使用'in'語句

這是我的查詢,我想算,如果任何比賽一樣mumbai然後發現爲mumbai:個別支數的

select TYPEOFPRODUCT, 
     SUM(CASE(BRANCH) WHEN IN('Mumbai1' OR 'Mumbai2') THEN 1 ELSE 0 END) AS 'Mumbai' 
     SUM(CASE(BRANCH) WHEN IN('Delhi1' OR 'DelhiMain') THEN 1 ELSE 0 END) AS 'Delhi' 
from los_ext 
group by TYPEOFPRODUCT 
+0

使用標識符,如列的別名,如雙引號'作爲「孟買」'。 (單引號用於字符串文字。) – jarlh

回答

1

你被關閉,你只需要以逗號分隔值IN而不是使用OR,並且CASE表達式有兩種格式,將該字段與之後的條件相關聯更加靈活,所以我更喜歡只使用它:

SELECT TYPEOFPRODUCT 
     ,SUM(CASE WHEN BRANCH IN('Mumbai1','Mumbai2') THEN 1 ELSE 0 END) AS Mumbai 
     ,SUM(CASE WHEN BRANCH IN('Delhi1', 'DelhiMain') THEN 1 ELSE 0 END) AS Delhi 
FROM los_ext 
GROUP BY TYPEOFPRODUCT 
0

它的查詢語句select和

select TYPEOFPRODUCT, 
SUM(CASE WHEN (BRANCH = 'Mumbai1' OR BRANCH = 'Mumbai2') THEN 1 ELSE 0 END) AS 'Mumbai', 
SUM(CASE WHEN (BRANCH = 'Delhi1' OR BRANCH = 'DelhiMain') THEN 1 ELSE 0 END) AS 'Delhi' 
from los_ext 
group by TYPEOFPRODUCT