目标:找到所有评级都不是A到D的所有属性
问题:如果某个属性有多个不是A-D的评级,那么这些仍然会计算在内。
代码:
Select DISTINCT (PropertyID)
FROM TableA
WHERE Rating NOT BETWEEN 'A' AND 'D'
答案 0 :(得分:4)
Select PropertyID
FROM TableA
group by PropertyID
having sum(case when Rating IN ('A','B','C','D') then 1 else 0 end) = 0
如果您只想要属性数量,那么就计算一下
select count(*) as property_count
from
(
select PropertyID from tableA ...
) alias_name