You can do this without uniting with leftist unions (I would advise to use explicit association anyway as implicit association for 20 years out of date Aaron Bertrand wrote a good blog on why ). Group_Type can become a join condition, meaning that the table is joined only if the correct group type is found:
SELECT ug.ID, ARRAY_AGG(COALESCE(a.Name, b.Name)) FROM User_Groups ug LEFT JOIN group_A a ON a.ID = ug.group_ID AND ug.Group_Type = 'A' LEFT JOIN group_B b ON b.ID = ug.group_ID AND ug.Group_Type = 'B' WHERE COALESCE(a.ID, b.ID) IS NOT NULL
However, I would be inclined to use UNION Still, but move it like this:
SELECT ug.ID, ARRAY_AGG(Name) FROM User_Groups ug INNER JOIN ( SELECT 'A' AS GroupType, ID, Name FROM Group_A UNION ALL SELECT 'B' AS GroupType, ID, Name FROM Group_B ) G ON g.GroupType = ug.Group_Type AND g.ID = ug.Group_ID GROUP BY ug.ID;
Added your script with my requests
source share