i have this table performances
, where there are performances of theatre plays from other table:
id | play_id | when | other uninteresting meta data
1 | 3 | 2020-04-01 | ...
2 | 4 | 2020-03-03 |
3 | 3 | 2020-01-02 |
4 | 1 | 2020-06-03 |
5 | 4 | 2020-10-13 |
and i would like to select the earliest performance for each distinct play (so each play is represented by a single, earliest, performance), ordered from soonest to latest.
so from the presented data i would like to get this:
id | play_id | when | other uninteresting meta data
3 | 3 | 2020-01-02 | ...
2 | 4 | 2020-03-03 |
4 | 1 | 2020-06-03 |
so far, after studying some answers here and here i came up with this query
SELECT * FROM
(
SELECT DISTINCT ON (play_id) *
FROM performances
WHERE performances.deleted_at is null
ORDER BY performances.play_id ASC, performances.when ASC
) distinct_plays
order by distinct_plays.when ASC
however, i don't trust it at all, because in the linked threads there were people bickering and telling that each other's answers are wrong; and while i see some problems with the answers in that thread i don't see a problem in this answer yet.
is this a good query for my task? does it not select duplicate rows or is very ineffective?