2014-04-18 91 views
1

2列計數我有一個名爲「訂單」需要查詢一次

它具有以下字段的表: 訂單ID,訂購日期,.....,市,StatusID

我想這個結果作爲回報:

City  No. of Delivered Orders, No. of Pending (Not Delivered) 
------------------------------------------------------------------- 
London  3      4 
Paris  5      6 
New York 7      8 

由於我們只有一個字段來跟蹤交貨狀態是StatusID,所以我面臨的困難,以計數同時兩個條件..

thanx提前:)

回答

1
select City, 
sum(case when StatusID = 'delivered' then 1 else 0 end) as [No. of Delivered Orders], 
sum(case when StatusID = 'not_delivered' then 1 else 0 end) as [No. of Pending] 
from Orders 
+0

這就是我想要的。謝謝 :) –