79629681

Date: 2025-05-20 01:55:31
Score: 0.5
Natty:
Report link

The query burns down to just:

SELECT *
FROM  ( 
   SELECT DISTINCT ON (initial_record_id)
   FROM   marketplace_messages
   ORDER  BY initial_record_id, id DESC
   ) sub
ORDER  BY id DESC;

The tricky bit is that leading ORDER BY expressions must cover DISTINCT ON expressions. So sort a second time in the outer query.

See:

Reasons:
  • Probably link only (1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • High reputation (-2):
Posted by: Erwin Brandstetter