用分组查询连接字符串



我有以下表格

<表类> row_num ref 价值 sub_value tbody><<tr>1ref1value1sub12ref1value1sub21ref2value1sub22ref2value2sub13ref2value2sub34ref2value4sub11ref2value1sub22ref2value2sub1

添加string_agg():

select ref, string_agg(value)
from t
group by ref
having countif(value = 'value1') > 0 and
countif(value = 'value2' or sub_value = 'sub1') > 0;

也就是说,我推荐使用array_agg(),这样您就有了一个值数组,而不是字符串。

最新更新