-1

When I use DISTINCT ON in PostgreSQL (distinct in django) which rows are retrieved in the group of rows with same fields?

mohammad
  • 2,232
  • 1
  • 18
  • 38

1 Answers1

3

The documentation says:

A set of rows for which all the expressions are equal are considered duplicates, and only the first row of the set is kept in the output. Note that the "first row" of a set is unpredictable unless the query is sorted on enough columns to guarantee a unique ordering of the rows arriving at the DISTINCT filter.

So if you add an ORDER BY clause, the first row in that order is kept.

Without an ORDER BY clause, there is no way of telling which row will be kept.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263