I have a table that includes each registration a user has completed for tournaments.
This table can have a user register for many tournaments over time.
It consists of user_id, tournament_id, and created_at date field
I need to see each users first tournament.
I tried the following:
SELECT user_id, MIN(created_at) AS first_participation_date, tournament_id
FROM participant_status
GROUP BY user_id, tournament_id
ORDER BY user_id, first_participation_date ASC;
But grouping on both ids will give me duplicates obvservations of users.
2
Answers
Not tested but:
See Distinct for more information.
A
FIRST_VALUE
window function with aDISTINCT
to remove the duplicates will do what you want.Alternatively, you can work with the
ROW_NUMBER()
,RANK()
orDENSE_RANK()
window functions.The query is a little bit more complex but much more flexible: it allows you to select the firsts
N
tournaments instead of only the very first one and the different window functions handle ties (= several registrations on the same day) differently from one another.Here is the version with
ROW_NUMBER()
.