查询以计算列中的值的数量

时间:2014-07-12 15:47:28

标签: sql sql-server

我有一个叫做应用程序的表。使用此列(app_status),其中只能接受这三个值中的一个“已接受”,“已拒绝”或待处理。我想显示每个状态的数量。换句话说,我想要一个能给我类似的查询:

application_status     :     number_of_status
--------------------------------------------
accepted               |         4
--------------------------------------------
rejected:              |         5
--------------------------------------------
pending:               |         10

在Microsoft SQL Server中

3 个答案:

答案 0 :(得分:1)

请使用GROUP BY

SELECT app_status, COUNT(app_status) AS NumberOfStatus
FROM application
GROUP BY app_status HAVING (COUNT(app_status)>1)
ORDER BY NumberOfStatus DESC

答案 1 :(得分:1)

试试这个:

Select count(case when app_status= 'accepted' then 1 else null end) as Accepted, 
count(case when app_status='rejected' then 1 else null end) as Rejected, 
count(case when app_status='pending'then 1 else null end) as Pending
From Application

- 吉姆

答案 2 :(得分:0)

更简单一点:

select application_status, count(application_status) as number_of_status 
from application group by application_status