sql 查询 去掉 某行为0的值
我查询数据时,大概比方,有3个列,A B C
都有很能为0 ,可能是 0 0 1 (不去掉)
也可能是 0 2 1 (不去掉)
也可能是 1 2 1 (不去掉)
也可能是 0 0 0 (去掉)
也可能是 0 2 0 (不去掉)
也可能是 0 0 0 (去掉)
,我所需要的就是 ,把所有为 0 的行,就是只有当 A B C都为0 的时候,才把这行去掉,不都为0 的话就不去掉
因为我要用
case when 统计 ,所以最后的数据为 1 6 3
该怎么实现呢?? 求指点
[最优解释]
看你的1,6,3是A,B,C三列分别的总和,那还去掉为0的做什么,又不影噢,呵呵,不懂
--这不就是1,6,3了嘛
select sum(A) A,sum(B) B,sum(C) C from 你的表;
declare @T table (A int,B int,C int)
insert into @T
select 0,0,1 union all
select 0,2,1 union all
select 1,2,1 union all
select 0,0,0 union all
select 0,2,0 union all
select 0,0,0
--如果数据中有1,-1,0,这样的判断就不对了。
select * from @T where A+B+C<>0
/*
A B C
----------- ----------- -----------
0 0 1
0 2 1
1 2 1
0 2 0
*/
--第一种方式
select * from @T where
(case when A=0 then 1 else 0 end +
case when B=0 then 1 else 0 end +
case when C=0 then 1 else 0 end )<>3
--第二种方式
select * from @T
except
select * from @T where A=0 and B=0 and C=0
--第三种方式
select * from @T t
where not exists
(select top 1 * from @T where t.A=0 and t.B=0 and t.C=0)
select * from 你的表 where (A+B+C)>0
SELECT * FROM TABLE01 WHERE (A<>0 AND B<>0 AND C<>0)