I am trying to create a SQL query that will combine Count with Case. My existing table and query is as follows:
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)
Which gives me this:
SPStatus SPPct
0 10
1 83
2 7
What I need is to create a Case subquery for the above query to display 0,1, 2 as No, Yes, and In Progress like this:
SPStatus SPPct
No 10
Yes 83
In Progress 7
Thanks, Robert
(CASE Surv_Permit
WHEN 0 THEN 'No'
WHEN 1 THEN 'Yes'
ELSE 'In Progress' END) as SPStatus
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.