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ピボット。質問を列としてリストし、ユーザーごとに1行に回答します

于 2012-06-01T11:15:45.920 に答える