代码如下:
with A as(select '工种' as 工种, case when INSTR(工种,'、')=0 then 工种 else SUBSTR(工种,0,INSTR(工种,'、')-1) end as 种类,1 as 数量 from 班组资源库) ,B as( select 工种, sum(数量) as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='木工' GROUP BY 工种 union all select 工种, 0 as 木工 ,sum(数量) as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='铝模' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,sum(数量) as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='钢筋工' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,sum(数量) as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='混凝土' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,sum(数量) as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='架子工' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,sum(数量) as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='砌筑' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,sum(数量) as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='抹灰' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,sum(数量) as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='瓦工' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,sum(数量) as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='机电' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,sum(数量) as 大白 ,0 as 粉刷石膏 ,0 as 其他 from A where 种类='大白' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,sum(数量) as 粉刷石膏 ,0 as 其他 from A where 种类='粉刷石膏' GROUP BY 工种 union all select 工种, 0 as 木工 ,0 as 铝模 ,0 as 钢筋工 ,0 as 混凝土 ,0 as 架子工 ,0 as 砌筑 ,0 as 抹灰 ,0 as 瓦工 ,0 as 机电 ,0 as 大白 ,0 as 粉刷石膏 ,sum(数量) as 其他 from A GROUP BY 工种 ) select "工种",sum(木工) as 木工,sum(铝模) as 铝模,sum(钢筋工) as 钢筋工 ,sum(混凝土) as 混凝土,sum(架子工) as 架子工,sum(砌筑) as 砌筑,sum(抹灰) as 抹灰 ,sum(瓦工) as 瓦工,sum(机电) as 机电,sum(大白) as 大白,sum(粉刷石膏) as 粉刷石膏,sum(其他)-(sum(木工)+sum(铝模)+sum(钢筋工)+sum(混凝土)+sum(架子工)+sum(砌筑)+sum(抹灰)+sum(瓦工)+sum(机电)+sum(大白)+sum(粉刷石膏)) as 其他 from B group by 工种 |