如果我把这个子查询,其选择支付任何项目销售人员和他们的最高价,他们卖:SQL:提高加盟效率
:select *,
(select top 1 highestProductPrice
from orders o
where o.salespersonid = s.id
order by highestProductPrice desc) as highestProductPrice
from salespersons s
在这个
,以提高工作效率加盟
select *, highestProductPrice
from salespersons s join (
select salespersonid, highestProductPrice, row_number(
partition by salespersonid
order by salespersonid, highestProductPrice) as rank
from orders) o on s.id = o.salespersonid
它仍然触及每一个订单记录(它通过salespersonid似乎滤波前枚举整个表。)但是你不能做到这一点:
select *, highestProductPrice
from salespersons s join (
select salespersonid, highestProductPrice, row_number(
partition by salespersonid
order by salespersonid, highestProductPrice) as rank
from orders
where orders.salepersonid = s.id) o on s.id = o.salespersonid
连接中的where子句导致“多部分标识符”s.id“无法绑定。
有什么方法可以通过连接将每个订单组中的前1个连接起来,但没有触及订单中的每条记录?
你是什么意思'触及每个记录'?它将不得不触及每一条记录**以找出哪一个需要成为顶级1 **。如果你没有按照你想要的顺序存在*索引,那么每次都必须在运行时执行(当然,它可能会缓存答案,或者构建一个可重用的临时索引)。 –
当您将枚举筛选为仅可连接记录时,查询所花费的时间显着减少。换句话说,只对我想要排名的记录进行排名,而不排序基于salespersonid不会加入的记录 –