I'm having a table like this
Movie | Actor |
---|---|
A | 1 |
A | 2 |
A | 3 |
B | 4 |
I want to get the name of a movie and all actors in that movie, and I want the result to be in a format like this:
Movie | ActorList |
---|---|
A | 1, 2, 3 |
How can I do it?
I'm having a table like this
Movie | Actor |
---|---|
A | 1 |
A | 2 |
A | 3 |
B | 4 |
I want to get the name of a movie and all actors in that movie, and I want the result to be in a format like this:
Movie | ActorList |
---|---|
A | 1, 2, 3 |
How can I do it?
Simpler with the aggregate function string_agg()
(Postgres 9.0 or later):
SELECT movie, string_agg(actor, ', ') AS actor_list
FROM tbl
GROUP BY 1;
The 1
in GROUP BY 1
is a positional reference and a shortcut for GROUP BY movie
in this case.
string_agg()
expects data type text
as input. Other types need to be cast explicitly (actor::text
) - unless an implicit cast to text
is defined - which is the case for all other string types (varchar
, character
, name
, ...) and some other types.
As isapir commented, you can add an ORDER BY
clause in the aggregate call to get a sorted list - should you need that. Like:
SELECT movie, string_agg(actor, ', ' ORDER BY actor) AS actor_list
FROM tbl
GROUP BY 1;
But it's typically faster to sort rows in a subquery. See:
SELECT
list or with dynamic SQL.
Commented
Apr 6, 2013 at 11:54
actor::TEXT
if actor
is an INT
. At least, I get an error trying to string_agg
INT
s in Postgres 9.5 - but otherwise, this was exactly what I needed, thanks!
Commented
Dec 13, 2016 at 16:26
ORDER BY
clause can go into the string_agg
function after the delimiter argument, e.g. string_agg(actor, ', ' ORDER BY actor DESC)
You can use array_agg
function for that:
SELECT "Movie",
array_to_string(array_agg(distinct "Actor"),',') AS Actor
FROM Table1
GROUP BY "Movie";
Result:
MOVIE | ACTOR |
---|---|
A | 1,2,3 |
B | 4 |
See this SQLFiddle
For more See 9.18. Aggregate Functions