【问题标题】:Split column into multiple columns by criteria按条件将列拆分为多列
【发布时间】:2020-05-16 22:49:15
【问题描述】:

我有如下查询:

select
  table.date,
  table.shift,
  sum(table.value)
from
  db.table
where
  table.date >= date '2020-01-01' and
  table.filter = 'type'
group by
  table.date,
  table.shift
order by
  table.date,
  table.shift;

以这种方式返回数据:

date       | shift | sum(value)
-----------|-------|------------
2020-01-06 | 1     | 15
2020-01-06 | 3     | 12
2020-01-07 | 1     | 20
2020-01-07 | 2     | 38
2020-01-09 | 1     |  6
2020-01-09 | 2     | 22
2020-01-09 | 3     | 14
2020-01-10 | 1     | 17
2020-01-10 | 2     |  3
2020-01-10 | 3     | 10

我正在尝试这样,但我不知道如何:

date       | 1  | 2  | 3
-----------|----|----|----
2020-01-06 | 15 |    | 12
2020-01-07 | 20 | 38 |
2020-01-09 |  6 | 22 | 14
2020-01-10 | 17 |  3 | 10

【问题讨论】:

    标签: sql oracle oracle11g group-by pivot


    【解决方案1】:

    无需添加子查询或 CTE。您可以使用条件聚合对您的查询稍作修改来旋转数据集:只需从group by 子句中删除shift,然后在sum()s 中实现条件逻辑:

    select
      date,
      sum(case when shift = 1 then value end) shift1,
      sum(case when shift = 2 then value end) shift2,
      sum(case when shift = 3 then value end) shift3
    from
      db.table
    where
      date >= date '2020-01-01'
      and filter = 'type'
    group by date
    order by date
    

    注意:

    • 没有必要为列名添加前缀,因为单个表开始起作用。我删除了那些

    • date 是 Oracle 中数据类型的名称,因此不适合作为列名

    【讨论】:

    • 这个答案更符合我的需要,所以我会接受。没想到这么多fastest gun in the west,更像是我没有找到的某个问题的重复,或者“你搜索过吗?”之类的指责。谢谢。
    【解决方案2】:

    你可以做条件聚合:

    select t.date,
           sum(case when t.shift = 1 then t.value else 0 end),
           sum(case when t.shift = 2 then t.value else 0 end),
           sum(case when t.shift = 3 then t.value else 0 end)
    from db.table as t
    where t.date >= date '2020-01-01' and
          t.filter = 'type'
    group by t.date;
    

    【讨论】:

      【解决方案3】:

      你可以使用条件聚合

      with cte as
      (
      select
        table.date,
        table.shift,
        sum(table.value) as val
      from
        db.table
      where
        table.date >= date '2020-01-01' and
        table.filter = 'type'
      group by
        table.date,
        table.shift
      order by
        table.date,
        table.shift
      )
      select date, max(case when shift=1 then val end) as 1,
      max(case when shift=1 then val end) as 2,
      max(case when shift=1 then val end) as 3
      from cte
      group by date
      

      【讨论】:

        【解决方案4】:

        您可以按如下方式使用PIVOT

        SELECT
            *
        FROM ( SELECT
          table.date,
          table.shift,
          table.value
        from
          db.table
        where
          table.date >= date '2020-01-01' and
          table.FILTER = 'type' ) 
          PIVOT 
          ( SUM ( VALUE ) FOR SHIFT IN ( 1,2,3 ))
        ORDER BY date;
        

        干杯!!

        【讨论】:

          猜你喜欢
          • 2021-12-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-29
          • 1970-01-01
          相关资源
          最近更新 更多