簡體   English   中英

sql查詢以獲取具有多個條件的數據

[英]sql query to get data with multiple condition

表 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

我正在尋找 SQL 查詢來獲取不同的 A 值,該值對於所有 B 都具有 C 值零。 (即輸出將是 1 & 5)

您可以使用group byhaving

select a
from t
group by a
having min(c) = 0 and max(c) = 0;

您可以檢查 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)

你可以做 :

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) 

NOT EXISTS

select distinct a from tablename t
where not exists (
  select 1 from tablename 
  where 
    a = t.a
    and
    c <> 0
)

嘗試這個 :

 Select 
 A
 from #tmp
 group by A 
 Having 
 count(*)=count(case when c=0 then 0 else null end)

對於 SQL Server:

SELECT A FROM ta
EXCEPT
SELECT A FROM ta WHERE C <> 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM