带有case子查询的SQL count%语句

时间:2014-09-24 19:42:02

标签: sql sql-server count case

我正在尝试创建一个将Count与Case结合在一起的SQL查询。我现有的表和查询如下:

Table
Surv_Permit ONLINEoff
0           ONLINE
0           OFFLINE 
1           ONLINE
2           ONLINE
0           OFFLINE 

Query
SELECT
Surv_Permit AS SPStatus,
(COUNT(Surv_Permit)* 100 / (Select Count(Surv_Permit) From dbo.Features)) AS SPPct
FROM dbo.Features
WHERE (ONLINEoff ='ONLINE')
GROUP BY (Surv_Permit)

这给了我这个:

SPStatus  SPPct
0         10
1         83
2         7

我需要为上面的查询创建一个Case子查询,将0,1,2显示为No,Yes和In Progress,如下所示:

SPStatus      SPPct
No            10
Yes           83
In Progress   7

谢谢,罗伯特

2 个答案:

答案 0 :(得分:0)

(CASE Surv_Permit 
     WHEN 0 THEN 'No' 
     WHEN 1 THEN 'Yes' 
     ELSE 'In Progress' END) as SPStatus

答案 1 :(得分:0)

SELECT CASE Surv_Permit  
WHEN 0 THEN 'NO' 
WHEN 1 THEN 'YES'
ELSE 'In Progress' END AS SPStatus
,(COUNT(Surv_Permit)* 100 / (Select Count(Surv_Permit) From dbo.Features)) AS SPPct
FROM dbo.Features
WHERE (ONLINEoff ='ONLINE')
GROUP BY (Surv_Permit)