I have a requirement wherein the table structure I have looks like
ID Owner Id NAME
1 20 Name 1
1 21 Name 2
1 34 Name 3
2 10 Name 4
2 12 Name 5
3 100 Name 6
I need a query that would give me the results as
ID Owner ID Name
1 20 Name 1, Name2, Name 3
2 10 Name4, Name5
3 100 Name 6
Currently we do this on the codebehind, but I would ideally like to do this through SQL and see if that amounts to any performance improvement.
You did not mention your DBMS so I'm assuming PostgreSQL:
SELECT id,
min(owner_id) as lowest_id,
string_agg(name, ', ') as name_list
FROM the_table
GROUP BY id
min()
can be used on a string value - a_horse_with_no_name 2012-04-04 18:05
Owner Id
to show - RedFilter 2012-04-04 17:42