1.Unpivot列转行
源表:
SELECT YEAR,
period,
entity_code,
entity_desc,
dept_code,
dept_desc,
supplier_code,
supplier_desc,
business_type,
account,
VALUE
FROM stg_ts_advance_pay unpivot(VALUE FOR account IN(advance_cny AS
'预付款申请金额(人民币)',
contract_amount_cny AS
'预付款采购合同额(人民币)',
contract_amount_cny AS
'合同额(人民币)',
contract_amount_usd AS
'合同额(美元)'))
结果展示:(数据敏感,所以隐掉公司/部门/供应商等字段数据)
新产生的列名:Account/Value
2.Pivot行转列
注意事项:Pivot使用过程中需要包含聚合函数,不然会抛Ora-56902错误。
业务类型Business_Type包含两种值:自营出口、代理出口
SELECT *
FROM (SELECT batch_code,
YEAR,
period,
business_type,
advance_cny
FROM stg_ts_advance_pay) pivot(SUM(advance_cny) advance_cny FOR business_type IN('自营出口' AS
self_export,
'代理出口' AS
agency_export));