2013-06-11 77 views
-2

我已經產生輸出就像一個存儲過程:SQL Server過程需要修改

Success Fail Progress 
---------------------------- 
    1  2  3 

但我想輸出是:

Recieved Count 
---------------- 
success  1 
---------------- 
fail  2 
---------------- 
progress 3 

請有人可以幫助我從得到這個輸出我的sql服務器。

當前的SQL:

select 
    sum(case when status='AK' then 1 else 0 end) as 'SUCCESS', 
    sum(case when status='E' then 1 else 0 end) as 'FAILURE', 
    sum(case when status NOT IN('AK','E')then 1 else 0 end) as 'PENDING' 
from t 
where [rec_datetime] BETWEEN '2008-02-11' AND DATEADD(DAY,1,'2008-02-11') 
+2

你可以發表你的SELECT語句的代碼,因爲它是現在? – JamieA

+0

sum(status ='AK'then 1 else 0 end時的情況)爲'SUCCESS', sum(status ='E'then 1 else 0 end時的情況)爲'FAILURE', sum IN('AK','E')then 1 else 0 end)as'PENDING' from t where [rec_datetime] BETWEEN'2008-02-11'and DATEADD(DAY,1,'2008-02-11' ) – user1268906

+0

當然。更改您的存儲過程輸出。 – Kermit

回答

0

替換爲你的SELECT語句

select 'Success' as Received, sum(case when status='AK' then 1 else 0 end) as [count] 
from t where [rec_datetime] BETWEEN '2008-02-11' AND DATEADD(DAY,1,'2008-02-11') 
union all 
select 'Fail', sum(case when status='E' then 1 else 0 end) 
from t where [rec_datetime] BETWEEN '2008-02-11' AND DATEADD(DAY,1,'2008-02-11') 
union all 
select 'Progress' , sum(case when status NOT IN('AK','E')then 1 else 0 end) 
from t where [rec_datetime] BETWEEN '2008-02-11' AND DATEADD(DAY,1,'2008-02-11') 
+0

Thanku Jamie..it works .... :) – user1268906

+0

1個小問題Jamie ...正在使用'Between'之間的正確方法提取日期..我的主要動機是從日曆中選擇一個日期選擇記錄和找到匹配的數據..否則它給我零記錄,因爲它在那裏與時間戳 – user1268906

+0

之間是包容性的,所以如果您的數據包含rec_datetime是12-02-2008 00:00:00的記錄,它們將包含在您的結果。對於你的例子,如果你只想要11/02/2008的數據,那麼使用[rec_datetime]> ='2008-02-11'和[rec_datetime] <'2008-02-12' – JamieA

-1

強硬沒有看到您的代碼或數據,但這樣的事情可能工作:

select 'success' As Recieved, Success As [Count] from mytable 
union all 
select 'fail', Fail from mytable 
union all 
select 'progress', Progress from mytable 
+0

不錯,但一個簡單的問題:爲什麼你把一個[Count]不算數? – LifeScript

+0

這不是'from mytable' ..這是'fromStoredProc'' – Kermit

+0

和你認爲存儲過程是從哪裏查詢的?一張桌子可能? –

1

您可以使用UNPIVOT將列移動到行中:

SELECT b.[Received], b.[Count] 
FROM (SELECT[Success] = 1, [Fail] = 2, [Progress] = 3) a 
UNPIVOT ([Count] FOR [Received] IN ([Success], [Fail], [Progress])) b 

輸出

Received Count 
----------- ----------- 
Success  1 
Fail  2 
Progress 3