I'm building a system in which there are the following tables:
- Song
- Broadcast
- Station
- Follow
- User
A user follows stations, which have songs on them through broadcasts.
I'm building a "feed" of songs for a user based on the stations they follow.
Here's the query:
SELECT DISTINCT ON ("broadcasts"."created_at", "songs"."id") songs.*
FROM "songs"
INNER JOIN "broadcasts" ON "songs"."shared_id" = "broadcasts"."song_id"
INNER JOIN "stations" ON "broadcasts"."station_id" = "stations"."id"
INNER JOIN "follows" ON "stations"."id" = "follows"."station_id"
WHERE "follows"."user_id" = 2
ORDER BY broadcasts.created_at desc
LIMIT 18
As you can see here, I'm getting duplicate songs which is happening because I'm following two stations that BOTH have broadcasted that same song:

Note: shared_id is the same as id.
My question is: How do I modify this query so it will return only unique songs based on their id (or shared_id)?
"broadcasts"."created_at"in theSELECT?