一行多列转置

select t2.* from table1 as t1
pivot ([聚合函数] for [行转列的条件列] in ([与“行转列条件列”匹配]) t2;

聚合函数(sum,max,avg等),可以支持一或多个,但是需要使用别名(列:sum(t1.x1) a,sum(t1.x2) b)
行转列的条件列,如果是需要多个列,使用concat或者||拼接成一个列

表pro_log如下:

一行多列转置
年、月拼接作为组合条件
需要查询 qty和qty*price的的数据并转成列

select t2.* from (select code,qty,qty*price as amount,yearly||monthly as y_m
from pro_log) as t1 pivot (max(t1.qty) q, max(t1.amount) a for y_m in (‘201910’,‘201911’) t2;

结果:

一行多列转置