是否有更快的方法来执行以下查询?
我需要根据同一张表中另一列pod.podId
的分钟找到DueDate
。
DECLARE @sodi = 7 -- this is the function parameter
select top 1
pod.podId
from
sod
inner join
idt on sod.ItemID = idt .ItemID
inner join
pod on pod.ProductID = idt.ProductID and pod.DueDate is not null and (pod.OrderQty >= pod.ReceivedQty)
inner join
poh on poh.poID = pod.poId and poh.StatusID <>32
where
sod.soid = @sodi
and pod.DueDate = (select min (pod.DueDate)
from sod
inner join idt on sod.ItemID = id.ItemID
inner join pod on pod.ProductID = id.ProductID and pod.DueDate is not null and (pod.OrderQty >= pod.ReceivedQty)
inner join poh on poh.poID = pod.poId and poh.StatusID <> 32
where sod.sodID = @sodi)
答案 0 :(得分:1)
您可以在Order By
pod.DueDate
DECLARE @sodi = 7 -- this is the function parameter
select top 1
pod.podId
from
sod
inner join
idt on sod.ItemID = idt .ItemID
inner join
pod on pod.ProductID = idt.ProductID and pod.DueDate is not null and (pod.OrderQty >= pod.ReceivedQty)
inner join
poh on poh.poID = pod.poId and poh.StatusID <>32
where
sod.soid = @sodi
order by pod.DueDate
答案 1 :(得分:0)
select top 1 pod.podId
from sod
join idt
on sod.ItemID = idt .ItemID
and sod.soid = @sodi
join pod
on pod.ProductID = idt.ProductID
and pod.DueDate is not null
and (pod.OrderQty >= pod.ReceivedQty)
join poh on poh.poID = pod.poId
and poh.StatusID <> 32
order by pod.DueDate, pod.podId
我将条件移动到连接中,查询优化器变得更聪明