Skip to content
Advertisement

query to convert column to row in sql server

need to convert this column to row

select okeydata
 from [elo].[dbo].[objkeys] 
 where parentid 
 in 
  ( select parentid from
   [elo].[dbo].[objekte] inner join [elo].[dbo].[objkeys] 
   on objid = parentid and objmask like 26 and okeydata like 1 )

acutal output

okeydata  
1
a
a@a.com
london

need query to be

okeydata  
1  a a@a.com london

Advertisement

Answer

If you want four columns, you can use row_number() or pivot:

select max(case when seqnum = 1 then okeydata end) as col_1,
       max(case when seqnum = 2 then okeydata end) as col_2,
       max(case when seqnum = 3 then okeydata end) as col_3,
       max(case when seqnum = 4 then okeydata end) as col_4
from (select okeydata,
             row_number() over (order by (select null)) as seqnum
      from [elo].[dbo].[objkeys] 
      where parentid in (select parentid
                         from [elo].[dbo].[objekte] inner join
                              [elo].[dbo].[objkeys] 
                               on objid = parentid 
                         where objmask like 26 and okeydata like 1 
                        )
     ) o;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement