表
Code Person Value
001 a 10
001 Null 20
002 b 25
002 b 30
003 Null 54
004 a 35我需要得到这样的结果
code sum
002 55
004 35就是说所有person中为Null的code所属的value不予统计
某编码(code)下的所有person都为非空的时候进行分组合计
Code Person Value
001 a 10
001 Null 20
002 b 25
002 b 30
003 Null 54
004 a 35我需要得到这样的结果
code sum
002 55
004 35就是说所有person中为Null的code所属的value不予统计
某编码(code)下的所有person都为非空的时候进行分组合计
(
a1 char(10),
a2 char(10),
a3 char(10))insert into tt1
select '001','a',10
union all
select '001',null,20
union all
select '002','b',25
union all
select '002','b',30select * from tt1select a1,sum(cast(a3 as int)) from tt1 a where not exists(select 1 from tt1 b where a2 is null and b.a1=a.a1)
group by a1
union all select '001', Null, 20
union all select '002','b' , 25
union all select '002','b', 30
union all select '003', Null, 54
union all select '004','a', 35goselect code,sum(value) from info where code not in (select code from info where person is null) group by code
drop table info
sum(value) as [sum]
from 表
where code not in(select distinct code from 表 where person is null)
group by code