Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Postgres数组\u agg一行中新字符串处的每个值_Sql_Database_Postgresql - Fatal编程技术网

Sql Postgres数组\u agg一行中新字符串处的每个值

Sql Postgres数组\u agg一行中新字符串处的每个值,sql,database,postgresql,Sql,Database,Postgresql,我有这样的疑问 SELECT group_id, array_agg(element_id) FROM table GROUP BY group_id; 因此,我有这样的想法: group_id | array_agg 106 | {2147,2138,2144} 107 | {2132,2510,2139} 应该编写什么查询,因此结果可以这样描述: group_id | array_agg 106 |

我有这样的疑问

SELECT group_id, array_agg(element_id) FROM table
GROUP BY group_id;
因此,我有这样的想法:

    group_id | array_agg

    106      | {2147,2138,2144}
    107      | {2132,2510,2139}
应该编写什么查询,因此结果可以这样描述:

        group_id | array_agg

        106      | {2147
                 |  2138
                 |  2144}
        107      | {2132
                 | 2510
                 | 2139}

基本上,应该在客户端应用程序中格式化输出,但是您可以使用带有新行字符的
string\u agg()

select group_id, string_agg(element_id::text, e'\n') 
from my_table
group by group_id;

 group_id | string_agg 
----------+------------
      106 | 2147      +
          | 2138      +
          | 2144
      107 | 2132      +
          | 2510      +
          | 2139
(2 rows)