| 发表于:2007-08-17 14:49:243楼 得分:0 |
declare @a table(型号 varchar(10), 总数 int, 良品数 int, 不良品数 int, 过程 varchar(20)) insert @a select 'aaa ', 1030, 1000, 20 , '在线物料 ' union all select 'aaa ', 1060 ,1000 ,40 , '产出物料 ' union all select 'aaa ', 2000 ,1990 ,10 , '产出物料 ' union all select 'bbb ', 1090 ,1000 ,50 , '在线物料 ' union all select 'bbb ', 1100 ,1000 ,100 , '产出物料 ' select 型号, 总数=sum(总数), [良品数(产出)]=sum(case when 过程= '产出物料 ' then 良品数 else 0 end), [不良品数(产出)]=sum(case when 过程= '产出物料 ' then 不良品数 else 0 end), [损耗(产出)]=sum(case when 过程= '产出物料 ' then 总数-良品数-不良品数 else 0 end), [良品数(在线)]=sum(case when 过程= '在线物料 ' then 良品数 else 0 end), [不良品数(在线)]=sum(case when 过程= '在线物料 ' then 不良品数 else 0 end), [损耗(在线)]=sum(case when 过程= '在线物料 ' then 总数-良品数-不良品数 else 0 end) from @a group by 型号 --result /* 型号 总数 良品数(产出) 不良品数(产出) 损耗(产出) 良品数(在线) 不良品数(在线) 损耗(在线) ---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- aaa 4090 2990 50 20 1000 20 10 bbb 2190 1000 100 0 1000 50 40 (所影响的行数为 2 行) */ | | |
|