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:

sql not working distinct on join

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

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -