I am trying to create a report that has the latest member id in the first column and the old member id in the second column based on the timestamp in audits table below. Member ids are stored in master table and the members table. audit_id from the audits table is a foreign key in members table.
I tried to write the query below after researching but I am getting syntax errors.
SELECT
(SELECT mem.member_id
FROM members mem
JOIN audits aud ON mem.audit_id = aud.audit_id
ORDER BY aud.update_time DESC FETCH FIRST 1 ROWS ONLY) AS latest_id,
mem1.member_id AS old_id
FROM members mem1
JOIN master ma ON ma.member_id = mem1.member_id
**audits**
audit_id |update_time
656566 |2021-01-15 17:01:34
656565 |2020-05-13 17:01:34
545245 |2020-08-11 17:01:34
**members**
member_id |audit_id
5655656 |656566
653363 |656565
545454 |545245
**master**
id | member_id
545634 | 5655656
545634 | 653363
545634 | 545454
**Expected output**
latest_id | old_id
5655656 |653363
5655656 |545454
Asked by Megatron
(11 rep)
Feb 16, 2021, 09:35 PM
Last activity: Apr 23, 2025, 10:06 AM
Last activity: Apr 23, 2025, 10:06 AM