0
我想優化此查詢使用聯接,而不是此子查詢,但與Top 1
,我有點困惑。我如何能移動此選擇語句與頂部
SELECT
s.ItemNumber
s.ImportKey
,(
SELECT top 1 MerchandiseGroupID
FROM MerchandiseGroup mg
WHERE s.StoreDepartment = mg.Name AND c.ClientNumber = s.ClientNumber
) as MerchandiseGroupID
FROM dbo.Source s
INNER JOIN dbo.Client c on s.ClientNumber = c.ClientNumber
INNER JOIN dbo.ClientVendor cv on s.Vendor = cv.ClientVendorName
INNER JOIN dbo.TypeClientWarehouse tw on c.WarehouseCode = tw.WarehouseCode
WHERE s.ImportDate > '2014-05-15 01:00:00.000'
我到目前爲止有:
INNER JOIN
(
SELECT
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY MerchandiseGroupID asc) rnum,
MerchandiseGroupID,
Name
FROM MerchandiseGroup mg
) mhg
ON s.StoreDepartment = mg.Name AND c.ClientNumber = s.ClientNumber
WHERE s.ImportDate > '2014-05-15 01:00:00.000' AND mg.rnum = 1
不管你採用什麼方法,子查詢中的where子句將使其運行得更快。 –