比如 表一 ID 部门 材料 数量,表二 部门  材料1的数量  材料2的数量  材料3的数量。要求:把表一 变成表二就是行转列。

 
基本写法:
selcet 部门,sum(case 材料 when 材料1 then 数量 else 0 end) ‘材料1的数量’,sum(case 材料 when 材料2 then 数量 else 0 end) ‘材料2的数量’,sum(case 材料 when 材料3 then 数量 else 0 end) ‘材料3的数量’ from 表一 group by 部门
 
优化写法:
select * from (select 部门,材料,数量 from 表一) pivot (count(数量) for 部门 in(‘部门1','部门2','部门3'))
posted on 2018-07-12 17:26  潇潇烟雨  阅读(172)  评论(0编辑  收藏  举报