When I use DISTINCT ON
in PostgreSQL (distinct in django) which rows are retrieved in the group of rows with same fields?
Asked
Active
Viewed 85 times
-1

mohammad
- 2,232
- 1
- 18
- 38
-
2the one that comes at first – Exprator Dec 12 '17 at 07:38
-
2The first value – Tomm Dec 12 '17 at 07:38
-
4Distinct doesn't delete anything – Sayse Dec 12 '17 at 07:38
-
@Sayse by delete I mean which row is retrieved ? – mohammad Dec 12 '17 at 07:39
1 Answers
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
-
yes, i tried some but each time the result is changed. need order to get static result. – Nam Nguyễn Dec 12 '17 at 08:19