我一生都看不到我在这里缺少什么,但有人可以向我指出为什么第一个查询没有做我想要的,但第二个工作正常吗?我知道查询并不完全相同,但它们应该返回相同的 20 行,但它们没有。(查询填充查找单个表中最常见的前 20 项)
select distinct
rx.drug_class
from rx
where exists
(
select top 20
rx2.drug_class
,COUNT(distinct rx2.pat_id) as counts
,RANK() over(order by count(distinct pat_id) desc) as rn
from rx as rx2
--when the line below is commented out
--this subquery gives the correct answer
where rx.drug_class = rx2.drug_class
group by rx2.drug_class
)
这个工作正常
select distinct
rx.drug_class
from rx
where rx.drug_class in
(
select top 20 rx.drug_class
from rx
group by rx.drug_class
order by COUNT(distinct pat_id) desc
)
Exists 子查询中的 where 子句不起作用,是什么原因?