将列转置为行,但不使用oracle中的数据透视表

时间:2019-10-04 10:56:42

标签: sql oracle oracle11g pivot

我有一个带有雇员ID和薪水列的雇员表,但我想对该列进行转置,但不使用pivot关键字。

原始表

ID工资

1   10000               
2   20000               
3   30000               
4   40000               
5   50000               

在选择查询输出后,例如= >>

ID      1       2       3       4       5
------  -----   -----   -----   -----   -----
Salary  10000   20000   30000   40000   50000

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用条件聚合:

select 'Salary' as id,
       max(case when id = 1 then salary end) as salary_1,
       max(case when id = 2 then salary end) as salary_2,
       max(case when id = 3 then salary end) as salary_3,
       max(case when id = 4 then salary end) as salary_4,
       max(case when id = 5 then salary end) as salary_5
from t;