Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 按条件将列拆分为多列_Sql_Oracle_Oracle11g_Group By_Pivot - Fatal编程技术网

Sql 按条件将列拆分为多列

Sql 按条件将列拆分为多列,sql,oracle,oracle11g,group-by,pivot,Sql,Oracle,Oracle11g,Group By,Pivot,我有一个类似以下的问题: 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

我有一个类似以下的问题:

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

您可以使用条件聚合

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

您可以使用条件聚合

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

不需要添加子查询或CTE。您可以使用条件聚合来透视数据集,只需稍微修改查询:只需从
group by
子句中删除
shift
,然后在
sum()中实现条件逻辑

注:

  • 由于只使用一个表,因此不需要在列名前加前缀。我把那些拿走了

  • date
    是Oracle中数据类型的名称,因此不是一个很好的列名选择


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

      注:

      • 由于只使用一个表,因此不需要在列名前加前缀。我把那些拿走了

      • date
        是Oracle中数据类型的名称,因此不是一个很好的列名选择


      您可以执行条件聚合:

      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;
      

      您可以执行条件聚合:

      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;
      

      您可以按如下方式使用
      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;
      

      干杯

      您可以按如下方式使用
      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;
      

      干杯

      这个答案更符合我的需要,所以我会接受。我没想到会有这么一个,更像是我没发现的问题的复制品,或者是“你搜索过了吗?”之类的指控。谢谢。这个答案更适合我的需要,所以我会接受的。我没想到会有这么一个,更像是我没发现的问题的复制品,或者是“你搜索过了吗?”之类的指控。非常感谢。