If you’re getting a PostgreSQL error that reads “SELECT DISTINCT ON expressions must match initial ORDER BY expressions” when trying to run a query, it’s probably because the initial columns provided to your ORDER BY
clause are different to the ones provided to the DISTINCT ON
clause.
To fix this error, make sure the initial columns provided to the ORDER BY
clause are included in the DISTINCT ON
clause.