hemanth hemanth - 1 year ago 59
SQL Question

merge all rows columns into single column from joined table

I want to join a table which has multiple rows and need to merge one of the column from all the rows into single column.

select a.parent_id,a.parent_name,concat(b.child_name) from parent a
join children b on (a.parent_id=b.parent_id);

This should return all the parent rows and each parent row should have all its children's.

i am thinking to group with parent_id but getting multiple records (one record per child). What logic i can implement here apart from grouping to get all child's for a parent in single row.

Answer Source
SELECT a.parent_id, a.parent_name, STRING_AGG(b.child_name, ',') as Children
    Parent a
    INNER JOIN children b
    ON a.Id = b.ParentId