【问题标题】:transpose column into rows but without using pivot in oracle [duplicate]将列转置为行,但在 oracle 中不使用枢轴[重复]
【发布时间】:2020-02-02 17:05:37
【问题描述】:

我有一个包含员工 ID 和薪水列的员工表,但我想转置该列但不使用 pivot 关键字。

原表

身份证工资

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

【问题讨论】:

  • 为什么不使用PIVOT?这正是它的设计目的。这就像说我想在计算机上存储一些字符但我不允许使用字符串数据类型;如果您有适合这项工作的工具,那么您应该使用它。

标签: sql oracle oracle11g pivot


【解决方案1】:

您可以使用条件聚合:

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;

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    相关资源
    最近更新 更多