可以改进此MS-Access查询吗?
即。重复“SELECT TOP 1 col FROM Table2”行
如果表2中的curindex之前有索引, 查询返回Table2的前一个col值,
否则从表3返回col匹配id的值。
即。
如果curindex = 7返回值=表2中的221
如果curindex = 5返回的值=表3中的200
Table2 Table3
id index col id col
1 1 110 1 100
1 2 120 2 200
1 3 130 3 300
1 4 140
2 5 211
2 6 221
2 7 231
PARAMETERS [curindex] Short;
SELECT TOP 1
IIF (
( SELECT TOP 1 col
FROM Table2
WHERE index < [curindex]
AND id =
( SELECT id
FROM Table2
WHERE index = [curindex])
) ,
( SELECT TOP 1 col
FROM Table2
WHERE index < [curindex]
AND id =
( SELECT id
FROM Table2
WHERE index = [curindex])
ORDER BY index DESC
),
( SELECT TOP 1 col
FROM Table3
WHERE id =
( SELECT id
FROM Table2
WHERE index = [curindex])
)
) AS col
FROM Table2
ORDER BY index
答案 0 :(得分:0)
您可以通过减少结果查询来限制iff条件的成本。
即
SELECT TOP 1
IIF (
( SELECT TOP 1 col
FROM Table2
WHERE index < [curindex]
AND id =
( SELECT id
FROM Table2
WHERE index = [curindex])
) ,
( SELECT TOP 1 col
FROM Table2
WHERE index = [curindex])
),
( SELECT TOP 1 col
FROM Table3
WHERE id =
( SELECT id
FROM Table2
WHERE index = [curindex])
)
) AS col
FROM Table2
ORDER BY index