这个 MS-Access 查询可以改进吗?
即重复“SELECT TOP 1 col FROM Table2”行
如果Table2中curindex之前有索引,则查询返回Table2中col之前的值,
否则从 Table3 中返回 col 匹配 id 的值。
IE
如果 curindex = 7 返回值 = 来自 Table2 的 221
如果 curindex = 5 返回值 = 200 来自 Table3
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