日期:2014-05-17 浏览次数:20527 次
select ID,NAM,max(N1) N1,max(N2) N2,max(N3) N3 from (你union all后的结果) t group by t.ID,t.NAM;
------解决方案--------------------
--> 测试数据:@表1 declare @表1 table([ID] varchar(3),[NAM] varchar(3),[N1] int,[N2] int,[N3] int) insert @表1 select '001','AAA',80,0,0 --> 测试数据:@表2 declare @表2 table([ID] varchar(3),[NAM] varchar(3),[N1] int,[N2] int,[N3] int) insert @表2 select '001','AAA',0,50,0 --> 测试数据:@表3 declare @表3 table([ID] varchar(3),[NAM] varchar(3),[N1] int,[N2] int,[N3] int) insert @表3 select '001','AAA',0,0,30 SELECT ID,NAM,MAX(N1) N1,MAX(N2) N2,MAX(N3) N3 FROM ( SELECT * FROM @表1 UNION ALL SELECT * FROM @表2 UNION ALL SELECT * FROM @表3 )T GROUP BY ID,NAM /* ID NAM N1 N2 N3 ---- ---- ----------- ----------- ----------- 001 AAA 80 50 30 */