簡體   English   中英

如何在 Postgresql 中使用自定義數組獲取列值?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM