在 sql 服务器中将列转换为行的查询

query to convert column to row in sql server

需要将此列转换为行

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 )

实际输出

okeydata  
1
a
a@a.com
london

需要查询

okeydata  
1  a a@a.com london

如果你想要四列,你可以使用row_number()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;