34,588
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:@tb
declare @tb table([ID] int,[STCD] varchar(2),[TM] datetime,[Z] int,[Q] int)
insert @tb
select 1,'S1','2009-08-10 08:00:00',12,null union all
select 2,'S1','2009-08-10 08:00:00',null,25
select min(ID), STCD, TM , sum( Z ) Z, sum( Q ) Q
from @tb
group by STCD, TM
/*
STCD TM Z Q
----------- ---- ----------------------- ----------- -----------
1 S1 2009-08-10 08:00:00.000 12 25
(1 行受影响)
*/
select min(ID) as ID,STCD,TM,max(Z) as Z,max(Q) as Q
from tb
group by STCD,TM
select ID,STCD,TM,max(Z) as Z,max(Q) as Q
from tb
group by ID,STCD,TM