Newbie Postgres question.
I have the following tables
players: id, name, …
votes: id, player_id, week – i.e. in votes there will be multiple records for each player_id
I want to get name (from players), vs: week[] (from votes where player_id == players.id)
i.e. I want to know which weeks each player voted in
select players.name, votes.week
from public.votes
join public.players
ON votes.player_id = players.id
gets me the number of results in the votes in table, but that includes endless duplicates of name. I want 1 row per player, with the weeks in a list
Is that possible?
>Solution :
You could group by the players’ name and use array_agg to get an array of weeks:
SELECT players.name, ARRAY_AGG(votes.week)
FROM public.votes
JOIN public.players ON votes.player_id = players.id
GROUP BY playes.name