例如,像这样的查询:SELECT
w.Id,
w.JJ_Consignment,
w.JU_PK,
(SELECT Top 1 JS_Lead FROM #TMP_SHIP s WHERE s.JS_Lead=w.JS_Ship) As JS_Lead,
w.JS_Ship,
(SELECT Top 1 JK_Consol FROM #TMP_Vessel v WHERE v.JS_Ship=w.JS_Ship) As JK_Consol,
(SELECT Top 1 JV_Vessel FROM #TMP_Vessel v WHERE v.JS_Ship=w.JS_Ship) As JV_Vessel,
(SELECT Top 1 JV_voyage FROM #TMP_Vessel v WHERE v.JS_Ship=w.JS_Ship) As JV_voyage,
w.JU_Planned,
w.JU_ActualIn,
FROM
#TMP_WHSE w
其中,SELECT Top 1 JS_Lead FROM #TMP_SHIP s WHERE s.JS_Lead=w.JS_Ship
SELECT Top 1 JK_Consol FROM #TMP_Vessel v WHERE v.JS_Ship=w.JS_Ship
SELECT Top 1 JV_Vessel FROM #TMP_Vessel v WHERE v.JS_Ship=w.JS_Ship
SELECT Top 1 JV_voyage FROM #TMP_Vessel v WHERE v.JS_Ship=w.JS_Ship
这些都是select同一表且条件也相同,能否将这些语句优化、简化?提高执行效率?
SELECT w.Id, w.JJ_Consignment, w.JU_PK, s.JS_Lead, w.JS_Ship, v.JK_Consol, v.JV_Vessel, v.JV_voyage, w.JU_Planned, w.JU_ActualIn, FROM #TMP_WHSE w
inner join #TMP_SHIP s on s.JS_Lead=w.JS_Ship inner join #TMP_Vessel v on v.JS_Ship=w.JS_Ship
用好视图。