[英]sql query to get data with multiple condition
table ta having three columns表 ta 具有三列
A | B | C
1 |11| 0
1 |12| 0
1 |13| 0
2 |33| 5
2 |34| 10
2 |35| 78
5 |45| 0
5 |49| 0
5 |51| 0
8 |10| 0
8 |14| -1
8 |34| -2
I am looking the SQL query to fetch the distinct A value which is having C value ZERO for all the B .我正在寻找 SQL 查询来获取不同的 A 值,该值对于所有 B 都具有 C 值零。 (ie the output would be 1 & 5)
(即输出将是 1 & 5)
You can use group by
and having
:您可以使用
group by
并having
:
select a
from t
group by a
having min(c) = 0 and max(c) = 0;
You can check the average of C if it is 0 then all the C values are zero for B,您可以检查 C 的平均值,如果它为 0,则 B 的所有 C 值都为零,
SELECT A
FROM table
GROUP BY A
HAVING SUM(ABS(C))=0
试试这个:
select distinct A from [dbo].[table] where A not in ( select A from [dbo].[table] where C <> 0)
You can do :你可以做 :
select t.*
from table t
where t.c = 0 and
not exists (select 1 from #tm t1 where t1.a = t.a and t1.c < 0)
With NOT EXISTS
: NOT EXISTS
:
select distinct a from tablename t
where not exists (
select 1 from tablename
where
a = t.a
and
c <> 0
)
Try this :尝试这个 :
Select
A
from #tmp
group by A
Having
count(*)=count(case when c=0 then 0 else null end)
For SQL Server:对于 SQL Server:
SELECT A FROM ta
EXCEPT
SELECT A FROM ta WHERE C <> 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.