读书人

sql 查询 去掉 某行为0的值,该如何解决

发布时间: 2013-01-25 15:55:29 作者: rapoo

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 你的表;

[解决办法]
既然要去掉三列均为0的行,那直接对这3列分别进行sum操作就可以了
[解决办法]

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)

------解决方案--------------------


引用:
SQL code

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
……

叶子你都写出来了,我们怎么发挥嘛
[解决办法]
楼主怎么还不结贴啊。。。如果是数据量多肯定见个视图 好啊。。快啊。。。

读书人网 >SQL Server

热点推荐