我在这里遇到一些逻辑问题。试图得到S.ID不在我的子查询中的行数。
COUNT(CASE WHEN S.ID IN
(SELECT DISTINCT S.ID FROM...)
THEN 1 ELSE 0 END)
我收到了错误:
Cannot perform an aggregate function on an expression containing an aggregate or a subquery.
如何解决这个问题或替代方案?
答案 0 :(得分:5)
也许是这样的?
SELECT COUNT(*) FROM .... WHERE ID NOT IN (SELECT DISTINCT ID FROM ...)
答案 1 :(得分:2)
使用EXISTS
:
SELECT COUNT(t.*) FROM table1 t WHERE NOT EXISTS (SELECT * FROM table2 WHERE ID = t.ID)
答案 2 :(得分:1)
将以下构造与CTE一起使用。
with cte as
(
select
case
when S.ID in (SELECT DISTINCT S.ID FROM LookupTable) then 1
else 0
end
as SID
from MyTable)
select count(SID) as SIDCOUNT from cte;