2017-02-27 28 views
-2

私は以下のようなDB2テーブルにこのようなデータを持っています。DB2 Pivot(行と列)

Server Name Job ID Job Status 
SERVER_A 00000001 Success 
SERVER_A 00000002 Failure 
SERVER_A 00000003 Success 
SERVER_B 00000004 Failure 
SERVER_B 00000005 Failure 
SERVER_B 00000006 Failure 
SERVER_C 00000007 Success 
SERVER_C 00000008 Success 
SERVER_C 00000009 Success 

以下のように結果を表示する必要があります。

Server Name Success Failure 
SERVER_A 2 1 
SERVER_B 0 3 
SERVER_C 3 0 
+0

答えを探すのは気になりましたか? – Charles

+0

はい、私は関連する回答を見つけることができませんでした – Kevin

答えて

1

溶液1

select ServerName, 
     sum(case when JobStatus='Success' then 1 else 0 end) Success, 
     sum(case when JobStatus='Failure' then 1 else 0 end) Failure 
from yourtable 
group by ServerName 
+0

優秀!!すべてのソリューションが機能しました。どうもありがとうございます。感謝します。 – Kevin

+0

あなたは歓迎です:) – Esperento57

1

溶液2

select distinct ServerName, 
(select count(*) from yourtable f2 where f2.ServerName=f1.ServerName and f2.JobStatus='Success') Success, 
(select count(*) from yourtable f2 where f2.ServerName=f1.ServerName and f2.JobStatus='Failure') Failure 
from yourtable f1 
1

溶液3

select distinct f1.ServerName, f3.*, f4.* 
from yourtable f1 
inner join lateral 
(
    select count(*) as Success from yourtable f2 
    where f2.ServerName=f1.ServerName and f2.JobStatus='Success' 
) f3 on 1=1 
inner join lateral 
(
    select count(*) as Failure from yourtable f2 
    where f2.ServerName=f1.ServerName and f2.JobStatus='Failure' 
) f4 on 1=1 
1

溶液4

with StatServer as (
select f1.ServerName, f1.JobStatus, count(*) as nb 
from yourtable f2 
group by f1.ServerName, f1.JobStatus 
) 
select 
ifnull(f1.ServerName, f2.ServerName) as ServerName, 
ifnull(f1.nb, 0) as Success, 
ifnull(f2.nb, 0) as Failure 
from StatServer f1 full outer join StatServer f2 
on f1.ServerName=f2.ServerName and f1.JobStatus='Success' and f1.JobStatus='Failure'