I need to write a query which takes rows and converts it into columns - here's my table:
Count    fname   lname   id
-----------------------------
1        abc     def    20
2        pqr            20      
3        abc     xyz    20  
4        xyz     xyz    20
1        abc     def    21
1        pqr     xyz    22
2        abc     abc    22
This is the output I'm trying to produce:
id  fname  lname  fname  lname  fname  lname  fname  lname
-------------------------------------------------------------
20  abc    def    pqr    NULL   abc    xyz    xyz    xyz
21  abc    def    NULL   NULL   NULL   NULL   NULL   NULL   
22  abc    abc    NULL   NULL   NULL   NULL   NULL   NULL
The max value of count for each id is 4. I'm using Oracle 9i.
Here's another one you might have some luck with. I like @ThinkJet's but not sure how much decode costs (if more or less than this below.
SELECT
   T1.ID,
   T1.fname,
   T1.lname,
   T2.fname,
   T2.lname,
   T3.fname,
   T3.lname,
   T4.fname,
   T4.lname
FROM
      table T1
   LEFT JOIN
      table T2
   ON
         T1.ID = T2.ID
      AND T2.count = 2
   LEFT JOIN
      table T3
   ON
         T1.ID = T3.ID
      AND T3.count = 3
   LEFT JOIN
      table T4
   ON
         T1.ID = T4.ID
      AND T4.count = 4
WHERE
   T1.count = 1
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With