我在DB2表中有类似这样的数据,如下所示。
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 :(得分: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
答案 1 :(得分: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
答案 2 :(得分: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
答案 3 :(得分: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'