Sql 将列转换为行,但不使用oracle中的pivot
我有一个包含员工ID和薪资列的员工表,但我希望转换该列,但不使用Sql 将列转换为行,但不使用oracle中的pivot,sql,oracle,oracle11g,pivot,Sql,Oracle,Oracle11g,Pivot,我有一个包含员工ID和薪资列的员工表,但我希望转换该列,但不使用pivot关键字 原始表格 身份证工资 选择查询输出后,如=>> ID 1 2 3 4 5 ------ ----- ----- ----- ----- ----- Salary 10000 20000 30000 40000 50000 您可以使用条件聚合: select 'Salary' as id, max(case
pivot
关键字
原始表格
身份证工资
选择查询输出后,如=>>
ID 1 2 3 4 5
------ ----- ----- ----- ----- -----
Salary 10000 20000 30000 40000 50000
您可以使用条件聚合:
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;
您可以使用条件聚合:
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;
请参见此处:为什么不使用
PIVOT
?这正是它的设计目的。这就像说我想在计算机上存储一些字符,但不允许使用字符串数据类型;如果您有一个适合该工作的工具,那么您应该使用它。请参见此处:为什么不使用PIVOT
?这正是它的设计目的。这就像说我想在计算机上存储一些字符,但不允许使用字符串数据类型;如果你有适合这份工作的工具,那么你应该使用它。