I have an example table with Ids and Names, I would like to get a new table with unique Ids and the names of one column combined (grouped by ID). My data looks like this (SQL Server 2017 & 2019 – SSMS):
HouseId, Names 1, William 1, James 1, Mason 1, Ella 1, Jackson 1, Evelyn 2, Colton 2, Robert 3, Austin 3, Cooper 3, Parker 4, Daisy 5, Edward 5, River
My goal is to get the following output with a qry:
HouseId, Names 1, William, James, Mason, Ella, Jackson and Evelyn 2, Colton and Robert 3, Austin, Cooper and Parker 4, Daisy 5, Edward and River
Does anybody has a tip/suggestion how to do this easy without making it to complicated by a SQL code? The double values by houseid are between 2 and 60.
Thank you for helping me!
Advertisement
Answer
The desire for and
makes this quite tricky. You seem to care about the ordering of the names — preserving the ordering from the original table. To handle that, I will assume there is an ordering column:
select houseid, (case when count(*) = 1 then max(name) else concat(string_agg(case when id <> max_id then name end, ', ') within group (order by id), ' and ', max(case when id = max_id then name end) ) end) as names from (select t.*, max(id) over (partition by houseid) as max_id from t ) t group by houseid;
If you don’t have such an id to preserver the ordering, you can use name
instead. The results have the format you want, but without an ordering column, the ordering cannot be preserved.
Here is a db<>fiddle.