Consider the following table:
id | x | y
----+----+-----
1 | 10 | 100
1 | 20 | 120
1 | 30 | 130
2 | 10 | 130
2 | 20 | 130
2 | 30 | 130
3 | 20 | 130
(7 rows)
I would like to have a single arbitrary tuple of every id, with the corresponding x and y values. The following results are correct:
id | x | y
----+----+-----
1 | 10 | 100
2 | 20 | 130
3 | 20 | 130
Or:
id | x | y
----+----+-----
1 | 30 | 130
2 | 30 | 130
3 | 20 | 130
Neither id, x nor y are unique in any context.
How do I go about this in SQL (under PostgreSQL)? There might be some millions of ids.
Update:
Thanks, @Lukáš Lalinský. Quoting the PostgreSQL documentation you’ve linked to:
DISTINCT ON( expression [, …] ) keeps only the first row of each
set of rows where the given expressions evaluate to equal… Note that the “first row” of each set is
unpredictable unlessORDER BYis used to ensure that the desired row
appears first.
Using the
DISTINCT ONclause should do the job: