开发者

table values as table header

开发者 https://www.devze.com 2023-01-04 03:43 出处:网络
The query: select Escuser,Eslevel from WF_UserConfiguration is returning me the table bellow: ╔═════════════════════╗

The query:

select Escuser,Eslevel from WF_UserConfiguration  

is returning me the table bellow:

╔═════════════════════╗
║ Escuser     Eslevel ║
╠═════════════════════╣
║ A000        1       ║
║ A010        4       ║
║ A021        3       ║
║ ABCD        1       ║
║ C067        3       ║
║ C099        1       ║
║ C252        2       ║
╚═════════════════════╝

My problem is I want to get the following output

╔═════════════════════════════╗
║       1     2      3     4  ║
╠═════════════════════════════╣
║     A000  C252   A021  A010 ║
║     ABCD         C067       ║
║     C099                    ║
╚══════════开发者_如何学Go═══════════════════╝

The table headers 1, 2, 3 and 4 are EsLevel values of first query result.

How should I get the following result (I mean what query)?


The answer using pivot: See live demo

select 
[1],
[2],
[3],
[4] 
from
(
select 
  Escuser,
  Eslevel, 
  Row_number() over(partition by Eslevel order by escuser asc)  as r
from WF_UserConfiguration  
)src
pivot
( 
  max(escuser) 
  for Eslevel in 
   (
    [1],[2],[3],[4]
   )
)p
0

精彩评论

暂无评论...
验证码 换一张
取 消