postgresql-聚合函数string_agg、array_agg的妙用
string_agg(ex1, d1):直接把一个表达式变成字符串如果需要对ord进行分组,但是num值要在一个格子里:SELECT ord,string_agg(num||'','-' ORDER BY num) from cx.over_testgroup by ord;array_agg(e1):把表达式变成一个数组 一般配合 array_to_string() 函数使用;select or
·
string_agg(ex1, d1):直接把一个表达式变成字符串

如果需要对ord进行分组,但是num值要在一个格子里:
SELECT ord,string_agg(num||'','-' ORDER BY num) from cx.over_test
group by ord;

array_agg(e1):把表达式变成一个数组 一般配合 array_to_string() 函数使用;
select ord, array_agg(num ORDER BY num) from cx.over_test
group by ord;

去重。可以只获取排序号:
select array_agg(distinct ord ORDER BY ord) from cx.over_test;

对于数组的操作,跟java差不多,后面跟序号就可以取值,从1开始:
select ord,(array_agg(num ORDER BY num))[1] from cx.over_test
group by ord;

更多推荐


所有评论(0)