Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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 如何从多个具有相同id的行中选择值,并将其与一行分隔?_Sql_Postgresql_Postgresql 9.1_String Aggregation - Fatal编程技术网

Sql 如何从多个具有相同id的行中选择值,并将其与一行分隔?

Sql 如何从多个具有相同id的行中选择值,并将其与一行分隔?,sql,postgresql,postgresql-9.1,string-aggregation,Sql,Postgresql,Postgresql 9.1,String Aggregation,我想在一行中显示具有相同id的表的值,并用-或 row id | base_id | auth_id --------+---------+--------- 4 | 1 | 1 5 | 1 | 3 6 | 2 | 2 7 | 2 | 6 8 | 2 | 5 我期待的结果 row id | base_id | a

我想在一行中显示具有相同id的表的值,并用
-

 row id | base_id | auth_id
--------+---------+---------
      4 |       1 |       1
      5 |       1 |       3
      6 |       2 |       2
      7 |       2 |       6
      8 |       2 |       5
我期待的结果

 row id | base_id | auth_id
--------+---------+---------
      1 |       1 | 1-3
      2 |       2 | 2-6-5

使用
string\u agg
尝试此选项:

select
    row_number() over (order by base_id) row_id,
    base_id,
    string_agg(auth_id, '-' order by auth_id)
from your_table
group by
    base_id
order by
    base_id;
您可以使用串联和
行号()
窗口分析函数:

select row_number() over (order by base_id) as row_id, 
       base_id, string_agg(auth_id::varchar,'-' order by auth_id) as auth_id
  from tab
 group by base_id;

您使用的是MySQL还是Postgres?它们是不同的数据库。对不起,我错了,我在用Postgres。你真的还在用Postgres 9.1吗?在这种情况下,您应该立即计划升级到当前(支持的)版本。编译器说“函数字符串_agg()不存在”@RezaPratama yes,需要转换为varchar。