Advertisement 3 | 2022-02-14T08:12:39Z | UPDATED_BY_ADMIN
WITH records AS (
SELECT
a.title,
s.date,
t.status,
ROW_NUMBER() OVER (PARTITION BY a.id ORDER BY s.date DESC) rn
FROM adverts a
LEFT JOIN sys_advert_statuses s ON a.id = s.ad_id
LEFT JOIN sys_advert_status_types t ON s.status_id = t.id
WHERE
s.status_id IN (3, 5, 6)
AND a.id = 4
) SELECT title, date, status FROM records WHERE rn = 1;