需要查询一次计数2列

时间:2014-04-18 09:37:03

标签: sql-server count

我有一张名为" Orders"

的表格

它有以下字段: OrderID,OrderDate,.....,City,StatusID

我想把这个结果作为回报:

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

由于我们只有一个字段来跟踪状态ID的交付状态,因此我一次难以计算两个条件。

提前的Thanx:)

1 个答案:

答案 0 :(得分: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