简体   繁体   English

如何在 Postgresql 中使用自定义数组获取列值?

[英]How to get column value with customize array in Postgresql?

I have a query result like below in c_order table for c_order_id colunm.我在c_order列的c_order_id表中有如下查询结果。

c_order_id
----------
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
....
....
....

I need a query for below result.我需要查询以下结果。

c_order_id
----------
1001,1002,1003,
1004,1005,1006,
1007,1008,1009,
1010,....,....

or或者

c_order_id
-----------
1001,1002,1003,1004,
1005,1006,1007,1008,
1009,1010,....,....

I concern with array_to_string(array_agg(c_order_id), e'\\n') where i get an array result but in per array value in per new line.我关心array_to_string(array_agg(c_order_id), e'\\n')在那里我得到一个数组结果,但在每个新行中的每个数组值中。 Here i can't customize array value with new line.在这里,我无法使用新行自定义数组值。

Do two levels of aggregation:做两级聚合:

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;

Thank you Gordon Linoff.谢谢戈登·林诺夫。 But need little correction但需要一点修正

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM