0

我有一个在 DB2 中运行良好的 SQL 查询我的结果是

SERVICE   IN   OUT   INPROGRESS

ADSL      1     5      10
VOIP      15    12     11
IPTV      20    14     17

现在我想把它改成这样:

CLASS       ADSL   VOIP   IPTV

IN           1     5      10
OUT          15    12     11
INPROGRESS   20    14     17

尽管我的 SQL 看起来很简单,但我从未改变过它。如果有人知道,我将不胜感激。

我的 SQL 是

select  distinct 'ADSL' as SERVICE,

(select count(*) as In from ticket 
where 
(class='C1'  and
(servicesinfault='25'))),

(select count(*) as Out from ticket
where 
(class='C2' and
(servicesinfault='25'))),


(select count(*) as In_progress from ticket
where 
(class='C3' and
(servicesinfault='25')))

from ticket where servicesinfault = '25'

union all

select  distinct 'VoIP',

(select count(*) from ticket 
where
(class='C1'  and
(servicesinfault='26'))),

(select count(*) from ticket
where
(class='C2'  and
(servicesinfault='26'))),


(select count(*) from ticket
where 
(class='C3'  and
(servicesinfault='26')))

from ticket where servicesinfault = '26'

union all

select  distinct 'IPTV',

(select count(*) from ticket 

where 
(class='C1'  and
(ticket.servicesinfault='27'))),

(select count(*) from ticket
where 
(class='C2'  and
(servicesinfault='27'))),

(select count(*) from ticket
where 
(class='C3'  and
(servicesinfault='27')))

from ticket where servicesinfault = '27'
4

2 回答 2

1

就像备注一样,您的结果应该是:

CLASS       ADSL   VOIP   IPTV

IN           1     15     20
OUT          5     12     14
INPROGRESS   10    11     17

旋转的版本应该是这样的:

select distinct 'In' as CLASS,

                (select count(*) as 'ADSL'
                   from ticket
                  where (class = 'C1' and (servicesinfault = '25'))),

                (select count(*) as 'VoIP'
                   from ticket
                  where (class = 'C1' and (servicesinfault = '26'))),

                (select count(*) as 'IPTV'
                   from ticket
                  where (class = 'C1' and (servicesinfault = '27')))

  from ticket
 where class = 'C1'

union all

select distinct 'Out',

                (select count(*)
                   from ticket
                  where (class = 'C2' and (servicesinfault = '25'))),

                (select count(*)
                   from ticket
                  where (class = 'C2' and (servicesinfault = '26'))),

                (select count(*)
                   from ticket
                  where (class = 'C2' and (servicesinfault = '27')))

  from ticket
 where class = 'C2'

union all

select distinct 'InProgress',

                (select count(*)
                   from ticket

                  where (class = 'C3' and (ticket.servicesinfault = '25'))),

                (select count(*)
                   from ticket
                  where (class = 'C3' and (servicesinfault = '26'))),

                (select count(*)
                   from ticket
                  where (class = 'C3' and (servicesinfault = '27')))

  from ticket
 where class = 'C3'
于 2012-06-01T11:20:21.270 回答
0

您要查找的词是“PIVOT”。

如果您的 DBMS 不提供,您可以使用“穷人的支点”。

看看 usr 对这个问题的回答:

穷人的 SQL 枢轴。将问题列为列并在一行中列出每个用户的答案

于 2012-06-01T11:15:45.920 回答