title: SQL How to transform ROWS to COLUMNS and COLUMNS TO ROWS? db2
body: <p>I have an SQL query which works fine in DB2
  My result is</p>
  
  <pre><code>SERVICE   IN   OUT   INPROGRESS
  
  ADSL      1     5      10
  VOIP      15    12     11
  IPTV      20    14     17
  </code></pre>
  
  <p>Now I want to transform it to be like this:</p>
  
  <pre><code>CLASS       ADSL   VOIP   IPTV
  
  IN           1     5      10
  OUT          15    12     11
  INPROGRESS   20    14     17
  </code></pre>
  
  <p>Although it seems long my SQL is very simple but I never had transformed this. If someone knows I will be thankful.</p>
  
  <p>My SQL is</p>
  
  <pre><code>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'
  </code></pre>