1

I need to query all columns in a table of all customers, the main factor being the latest version for each customer.

My table:

enter image description here

My Query:

SELECT DISTINCT ON(code)
    code,
    namefile,
    versioncol,
    status
FROM table_A
    ORDER BY versioncol desc

Error:

ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions LINE 1: SELECT DISTINCT ON(code)

GMB
  • 216,147
  • 25
  • 84
  • 135
rufus05
  • 43
  • 1
  • 10

1 Answers1

0

Postgres' error message is trying to tell you what to do:

DISTINCT ON expressions must match initial ORDER BY expressions

Actually that's quite clear: to make your code a valid DISTINCT ON query, you just need to add code (that's the DISTINCT ON expression) as a first sorting criteria to the query (ie as initial ORDER BY).

SELECT DISTINCT ON(code) a.*
FROM table_A a
ORDER BY code, versioncol DESC
GMB
  • 216,147
  • 25
  • 84
  • 135