【问题标题】:How to display row data as column in OracleOracle中如何将行数据显示为列
【发布时间】:2021-04-26 03:42:29
【问题描述】:

我想将行值显示为列值。并在表格末尾显示最终总值。

为此,我使用以下数据集。

我想在列边设置这个数据。

我使用这个 SQL 查询来做到这一点。但我不知道如何获得 Hours Total 列

select *   
from 
    (select EMP_NO,SUM(Hours) total
     from Employee_Attendence
     group by EMP_NO) 
pivot
    (sum(total) 
         for WAGE_Type in ('Absence', 'Normal'))

最终输出应如下所示。

Select EMP_NO, Absence, Normal, Total
From 
    (select *
     from 
         (select EMP_NO, sum(Hours) total
          from Employee_Attendence
          group by EMP_NO) 
     pivot
         (sum(total) 
              for WAGE_Type in ('Absence', 'Normal'))
)

【问题讨论】:

    标签: oracle


    【解决方案1】:
    SELECT emp_id, sum( CASE  WHEN wage_type = 'Absence' THEN Hours END ) Absence,
                   sum( CASE  WHEN wage_type = 'Normal' THEN Hours END ) Normal,
                   sum( hours ) "Hours total"
    FROM employee_attendance
    GROUP BY emp_id;
    

    输出

    EMP_ID  ABSENCE NORMAL HOURS TOTAL
    4000    8       32     40
    

    【讨论】:

    • 太棒了。非常感谢
    猜你喜欢
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多