SELECT *
FROM [dbo].[CountData] CD
INNER JOIN [dbo].[Count] C ON CD.CountId = C.CountId
LEFT JOIN [dbo].[Leg] L ON C.IntersectionId = L.IntersectionId AND CD.ApproachId = L.ApproachId
WHERE L.IntersectionId IS NULL
如何重写此查询,以使不涉及LEFT JOIN。最终目标是要有一个可以作为索引视图接受的查询:https://www.brentozar.com/archive/2013/11/what-you-can-and-cant-do-with-indexed-views/(不允许左联接)
注意:不允许子查询
答案 0 :(得分:1)
您可以使用NOT EXISTS
重写它:
SELECT *
FROM [dbo].[CountData] CD
JOIN [dbo].[Count] C
ON CD.CountId = C.CountId
WHERE NOT EXISTS (SELECT 1 FROM [dbo].[Leg] L
WHERE C.IntersectionId = L.IntersectionId
AND CD.ApproachId = L.ApproachId)