2014-07-12 52 views
-2

我有表稱爲應用程序。在這一列(app_status)中,它可以只接受「接受」,「拒絕」或待定這三個值中的一個。我想顯示每個狀態的編號。在另一個字,我希望有一個查詢,這將使我是這樣的:查詢來計算列的值的數量

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

在Microsoft SQL Server

回答

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 

--Jim

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 
0

稍微簡單:

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