【问题标题】:Oracle Pivot Help based on Data基于数据的 Oracle Pivot 帮助
【发布时间】:2020-10-15 21:07:03
【问题描述】:

我正在尝试使用 oracle 数据透视函数 以以下格式显示数据。我尝试使用我在 stackoverflow 中找到的示例,但我无法实现我所期待的。

    With  t as
(
select 1335 as emp_id, 'ADD Insurance New' as suuid, sysdate- 10 as startdate, null as enddate from dual
union all
select 1335 as emp_id, 'HS' as suuid, sysdate- 30 as startdate, null as enddate from dual
union all
select 1335 as emp_id, 'ADD Ins' as suuid, sysdate- 30 as startdate, Sysdate - 10 as enddate from dual
) 
select * from t

输出:

+--------+-------------------+-------------------+---------+-------------------+
| EMP_ID |      SUUID_1      | SUUID_1_STARTDATE | SUUID_2 | SUUID_2_STARTDATE |
+--------+-------------------+-------------------+---------+-------------------+
|   1335 | ADD Insurance New | 10/5/2020 15:52   | HS      | 9/15/2020 15:52   |
+--------+-------------------+-------------------+---------+-------------------+

谁能建议如何使用 SQL Pivot 来获取这种格式?

【问题讨论】:

    标签: sql oracle datetime pivot


    【解决方案1】:

    您可以使用条件聚合。理解您的问题的方法不止一种,但适用于您的示例数据的一种方法是:

    select emp_id,
        max(case when rn = 1 then suuid end) suuid_1,
        max(case when rn = 1 then startdate end) suid_1_startdate,
        max(case when rn = 2 then suuid end) suuid_2,
        max(case when rn = 2 then startdate end) suid_2_startdate
    from (
        select t.*, row_number() over(partition by emp_id order by startdate desc) rn
        from t
        where enddate is null
    ) t
    group by emp_id
    

    Demo on DB Fiddle

    EMP_ID | SUUID_1 | SUID_1_STARTDATE | SUUID_2 | SUID_2_STARTDATE -----: | :---------------- | :--------------- | :-------- | :--------------- 第1335章添加保险 新 | 05-OCT-20 | HS | 20 年 9 月 15 日

    【讨论】:

      【解决方案2】:

      您可以使用PIVOT

      With  t ( emp_id, suuid, startdate, enddate ) as
      (
        select 1335, 'ADD Insurance New', sysdate- 10, null         from dual union all
        select 1335, 'HS',                sysdate- 30, null         from dual union all
        select 1335, 'ADD Ins',           sysdate- 30, Sysdate - 10 from dual
      )
      SELECT emp_id,
             "1_SUUID" AS suuid1,
             "1_STARTDATE" AS suuid_startdate1,
             "2_SUUID" AS suuid2,
             "2_STARTDATE" AS suuid_startdate2
      FROM   (
        SELECT t.*,
               ROW_NUMBER() OVER ( ORDER BY startdate DESC, enddate DESC NULLS FIRST )
                 AS rn
        FROM   t
      )
      PIVOT (
        MAX( suuid ) AS suuid,
        MAX( startdate ) AS startdate,
        MAX( enddate ) AS enddate
        FOR rn IN ( 1, 2 )
      )
      

      输出:

      EMP_ID | SUUID1 | SUUID_STARTDATE1 | SUUID2 | SUUID_STARTDATE2 -----: | :---------------- | :--------------- | :----- | :--------------- 第1335章添加保险 新 | 05-OCT-20 | HS | 20 年 9 月 15 日

      db小提琴here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-13
        • 2012-04-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多