Aggregating data
Question:
We would like to group our cats by color. Return 3 rows, each row containing a color and a list of cat names
Return: color, names
Order by: color DESC
Show Table
Cats:
| name | varchar |
| breed | varchar |
| weight | float |
| color | varchar |
| age | int |
Show Desired Output
Desired output:
| color | names |
| Tortoiseshell | Felix,Tigger,Millie,Puss |
| Brown | Alfie,Misty,Smokey |
| Black | Ashes,Molly,Smudge,Oscar,Charlie |
Query Window:
Correct output but can you use 'array_agg'?
Show Answer
Answer:
select color,
array_agg(name) as names
from cats group by color
order by color desc