首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql 查询 去掉 某举动0的值

2012-12-24 
sql 查询 去掉 某行为0的值我查询数据时,大概比方,有3个列,A B C都有很能为0 ,可能是0 0 1(不去掉)也可能

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

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

select * from 你的表 where (A+B+C)>0

[其他解释]
因为我的数据量很大啊,5W行,有4W是空的
[其他解释]
select * from 你的表 where (A+B+C)>0

[其他解释]
能从数据表里直接删除全零行吗?delete from t where a=0 and b=0 and c=0
[其他解释]
最好的办法是先创建视图,create view no_zero as select a,b,c from t where a<>0 and b<>0 and c<>0   然后 select sum(a) sum(b)sum(c) from no_zero 
[其他解释]
select sum(a) sum(b)sum(c) from no_zero where a<>0 and b<>0 and c<>0  直接这样行吗

[其他解释]

SELECT * FROM TABLE01 WHERE (A<>0 AND B<>0 AND C<>0) 

[其他解释]
引用:
select * from 你的表 where (A+B+C)>0


貌似可以了,还要求什么
[其他解释]
该回复于2012-09-28 15:16:50被版主删除
[其他解释]
引用:
SQL code


select * from 你的表 where (A+B+C)>0


或者 select * from @T where A=0 and B=0 and C=0

[其他解释]
用视图干啥?单表的话直接操作最快
[其他解释]
6楼的正解。

select * from 你的表 where (A+B+C)>0
这个如果a,b,c有负数,那就有问题了。
[其他解释]
引用:
看你的1,6,3是A,B,C三列分别的总和,那还去掉为0的做什么,又不影噢,呵呵,不懂

SQL code


--这不就是1,6,3了嘛
select sum(A) A,sum(B) B,sum(C) C from 你的表;
+1

热点排行