2014-09-24 52 views
0

我想創建一個SQL查詢,將結合計數與案例。我現有的表和查詢如下: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 

我需要的是建立一個案例子查詢以上查詢顯示0,1,2爲否定,是的,在這樣的進展情況:

SPStatus  SPPct 
No   10 
Yes   83 
In Progress 7 

謝謝,羅伯特

+1

這更多的是一個客戶端責任格式化數據供人類消費。但顯而易見的方法是創建一個具有'0-> No','1-> Yes'等等的分散表。映射並且只是'連接'到您的主查詢中。 – 2014-09-24 19:43:57

回答

0
(CASE Surv_Permit 
    WHEN 0 THEN 'No' 
    WHEN 1 THEN 'Yes' 
    ELSE 'In Progress' END) as SPStatus 
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)