sql - Postgres: Selecting Distinct on a column is not returning distinct results with Joins -
i'm building system in there following tables:
- song
- broadcast
- station
- follow
- user
a user follows stations, have songs on them through broadcasts.
i'm building "feed" of songs user based on stations follow.
here's query:
select distinct on ("broadcasts"."created_at", "songs"."id") songs.* "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" "follows"."user_id" = 2 order broadcasts.created_at desc limit 18
as can see here, i'm getting duplicate songs happening because i'm following 2 stations both have broadcasted same song:
note: shared_id same id.
my question is: how modify query return unique songs based on id (or shared_id)?
remove "broadcasts"."created_at"
distinct
predicate because returning non-unique values, hence duplicate songs
. if need use information sort please create new question.
Comments
Post a Comment