postgresqlsubquerymultiple-results

Results of Sub query in one column? Postgresql


I want to know if there is a way to get multiple results of a subquery in one column for the query... Something like this:

otherid | resultsFromsomeData
1       | 1,2,3   
2       | 1,5  
3       | No Data   

I was trying something like this:

select o.otherID, 
CASE WHEN (select otherID from someData where someid=o.otherID)>1 THEN
       ''||(select otherID from someData where someid=o.otherID)||'' /*here I am*/
   WHEN (select otherID from someData where someid=o.otherID)<1 THEN
       'No Data'
   END 
as resultsFromsomeData
from OtherData o



Solution

  • Looks like a simple join and group by:

    select o.other_id, 
           coalesce(string_agg(sd.other_id, ','), 'No data') as results_from_some_data
    from other_data o
      join some_data d on o.other_id = d.someid
    group by o.other_id;