![](/img/trans.png)
[英]With PostgreSQL, how would I get the value of column one with an item in an array?
[英]How to get column value with customize array in Postgresql?
我在c_order
列的c_order_id
表中有如下查询结果。
c_order_id
----------
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
....
....
....
我需要查询以下结果。
c_order_id
----------
1001,1002,1003,
1004,1005,1006,
1007,1008,1009,
1010,....,....
或者
c_order_id
-----------
1001,1002,1003,1004,
1005,1006,1007,1008,
1009,1010,....,....
我关心array_to_string(array_agg(c_order_id), e'\\n')
在那里我得到一个数组结果,但在每个新行中的每个数组值中。 在这里,我无法使用新行自定义数组值。
做两级聚合:
select string_agg(col_3, '\n')
from (select string_agg(col, ',') as col_3
from (select t.*,
row_number() over (order by col) - 1 as seqnum
from t
) t
group by floor(seqnum / 3)
) t;
谢谢戈登·林诺夫。 但需要一点修正
select string_agg(col_3, e'\n') c_order_id
from (select string_agg(c_order_id::character varying, ',') as col_3
from (select t.c_order_id,
row_number() over (order by c_order_id) - 1 as seqnum
from c_order t
) t
group by floor(seqnum / 4)
) c_order ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.