一、pivot 行转列
使用表dm_custbd_lab_c_month
表结构如下:
行转列语句:
select *
from (select t.c_month, t.c_channel, t.c_soto, t.label_code, t.label_value
from dm_custbd_lab_c_month t
where t.c_month = '201706'
and t.label_code in ('LAB_004', 'LAB_001', 'LAB_002')) pivot(max(label_value) for label_code in('LAB_004' as
LAB_004,
'LAB_001' as
LAB_001,
'LAB_002' as
LAB_002));
结果展示:
二、列转行 UNPIVOT
表结构展示:
列转行脚本:
select *
from (select t.c_month, t.c_channel, t.c_soto, t.symb00034, t.symb00015
from dm_custbd_k_c_month_hor t
where t.c_month = '201706') unpivot((kpi_value) for index_id in(SYMB00034,
SYMB00015))