-2

次のような出力を生成するストアド プロシージャがあります。

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') 
4

3 に答える 3

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
于 2013-06-11T13:53:44.400 に答える
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')
于 2013-06-11T13:50:39.040 に答える
-1

あなたのコードやデータを見なければ難しいですが、次のようなものがうまくいくかもしれません:

select 'success' As Recieved, Success As [Count] from mytable
union all
select 'fail', Fail  from mytable
union all
select 'progress',  Progress from mytable
于 2013-06-11T13:45:30.853 に答える