我为我的生活不能看到我在这里失踪,但能有人指出我为什么第一个查询不会做我想做的,但第二个工作正常?我知道查询不完全一样,但它们应该返回相同的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
)
where子句中已存在子查询不工作,怎么办?
你确定注释掉了哪里rx.drug_class = rx2.drug_class使它工作。删除主查询的链接将Exists子句转换为'EXISTS(SELECT * FROM rx)' – 2013-03-26 18:26:23