Abdullah Shaaban
Abdullah Shaaban

Reputation: 1

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
[email protected]
london

need query to be

okeydata  
1  a [email protected] london

Upvotes: 0

Views: 111

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

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;

Upvotes: 2

Related Questions