我之前已经意识到这一点,但我所看到的答案从未考虑过日期完全相同的情况。
我有下表汇款。
我需要公司+帐户
的每一组的最新转帐的 TRANSFER_ID这是我目前的查询:
SELECT tt.transfer_id,
tt.corporation,
tt.account,
tt.date
FROM Transfers tt INNER JOIN
(SELECT t.corporation,
t.account,
MAX(t.date) AS day_last_transfer
FROM Transfers t
GROUP BY t.corporation, t.account) j
ON tt.corporation = j.corporation
AND tt.account = j.account
AND tt.date = j.day_last_transfer
问题在于,当执行内部联接时,由于日期完全相同,我有时会使用不同的transfer_id来获得相同 CORPORATION + ACCOUNT 的2或3条记录
最后,我必须使用MS ACCESS来执行此查询
答案 0 :(得分:1)
然后选择 Max (或 Min )id:
SELECT
Max(tt.transfer_id) As last_transfer_id,
tt.corporation,
tt.account,
tt.date
FROM
Transfers tt
INNER JOIN
(SELECT t.corporation,
t.account,
MAX(t.date) AS day_last_transfer
FROM Transfers t
GROUP BY t.corporation, t.account) j
ON tt.corporation = j.corporation
AND tt.account = j.account
AND tt.date = j.day_last_transfer
GROUP BY
tt.corporation,
tt.account,
tt.date