【问题标题】:Results of Sub query in one column? Postgresql子查询的结果在一列中? PostgreSQL
【发布时间】:2020-08-28 20:33:59
【问题描述】:

我想知道是否有办法在查询的一列中获取子查询的多个结果... 像这样的:

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

我正在尝试这样的事情:

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


【问题讨论】:

    标签: postgresql subquery multiple-results


    【解决方案1】:

    看起来像一个简单的加入和分组:

    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;
    

    【讨论】:

    • 好吧,我不知道 String_AGG 和 Coalesce 非常感谢!救了我的命!
    猜你喜欢
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 2012-11-25
    • 2023-01-22
    • 2016-06-10
    • 2011-03-17
    相关资源
    最近更新 更多