【问题标题】:I have a column with active and inactive elements in same column; I need split active and inactive separately我在同一列中有一个包含活动和非活动元素的列;我需要分别拆分活动和非活动
【发布时间】:2021-07-21 21:54:45
【问题描述】:

我有一个名为History 的表,其中包含id, actiondate, status 列。

ID ActionDate Status
11 4/22/2021 active
11 4/25/2021 Deactive

我需要在 Oracle 的不同列中为一个 ID 向用户显示 activedate 和 deactivedate。

【问题讨论】:

    标签: sql oracle oracle11g


    【解决方案1】:

    你可以使用PIVOT:

    SELECT *
    FROM   history
    PIVOT (
      MAX( ActionDate )
      FOR Status IN (
        'active' As active,
        'Deactive' AS deactive
      )
    )
    

    或条件聚合:

    SELECT id,
           MAX( CASE status WHEN 'active' THEN ActionDate END ) AS active,
           MAX( CASE status WHEN 'Deactive' THEN ActionDate END ) AS Deactive
    FROM   history
    GROUP BY
           id;
    

    其中,对于样本数据:

    CREATE TABLE history ( ID, ActionDate, Status ) AS
    SELECT 11, DATE '2021-04-22', 'active' FROM DUAL UNION ALL
    SELECT 11, DATE '2021-04-25', 'Deactive' FROM DUAL
    

    输出:

    ID ACTIVE DEACTIVE
    11 2021-04-22 00:00:00 2021-04-25 00:00:00

    db小提琴here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 2019-11-11
      • 1970-01-01
      • 2022-11-18
      • 2018-01-31
      • 1970-01-01
      相关资源
      最近更新 更多