将行转换为列

Convert Rows into Cols

我有以下 table 调用 fc_summary:

CALMONTH   COMP_CODE    FC10    FC20    FC30
201901     C001         550      45     100
201901     C002         50       450    10
201901     C003         150      250    120
201901     C004         520      410    10
201901     C005         30       40     105

我想从中创建 my_summary table 喜欢

CALMONTH   CODE     AMOUNT
201901     FC10     1300
201901     FC20     1195
201901     FC30     345

我尝试使用 PIVOT 实现此目的,但无法理解

您可以使用这个查询

with t(CALMONTH, COMP_CODE, FC10, FC20, FC30) as
(
 select 201901,'C001',550,45,100  from dual union all
 select 201901,'C002',50,450,10   from dual union all
 select 201901,'C003',150,250,120 from dual union all
 select 201901,'C004',520,410,10  from dual union all
 select 201901,'C005',30,40,105   from dual
)
select CALMONTH, FC as CODE, SUM(VALUE) as VALUE
  from t
 unpivot 
 (
  VALUE for FC in (FC10,FC20,FC30)
 )  
 group by CALMONTH, FC
 order by FC;

CALMONTH    CODE    VALUE
201901      FC10    1300
201901      FC20    1195
201901      FC30    345

取消数据透视