這是一個「爲例套,內集「子查詢。最好的方法是使用having
子句進行聚合,因爲它是最通用的方法。這會產生這樣的id的列表:
select id
from t
group by id
having SUM(case when colX = 20 then 1 else 0 end) = 0 and -- colX is never 20
SUM(case when colY = 31 then 1 else 0 end) = 0 -- colY is never 31
您可以使用子查詢數數:
select count(*)
from (select id
from t
group by id
having SUM(case when colX = 20 then 1 else 0 end) = 0 and -- colX is never 20
SUM(case when colY = 31 then 1 else 0 end) = 0 -- colY is never 31
) s
對於第二種情況,你會:
select count(*)
from (select id
from t
group by id
having SUM(case when colX = 20 then 1 else 0 end) > 0 and -- colX has at least one 20
SUM(case when colY = 31 then 1 else 0 end) > 0 -- colY has at least one 31
) s
[你有什麼想法](http://mattgemmell.com)/2008/12/08 /什麼具備的,你試了/)?這是一項家庭作業嗎? – creinig
這不是功課。我在另一個select查詢中嘗試了select語句,但我沒有得到我想要的結果 – Anonymous