1

i'd like to display the values of the table with the same id in just one row and separate them with - or,

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

result i expect

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

2 Answers2

1

Try this option using string_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;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

You can use string_agg() concatenation and row_number() window analytic function :

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;

Demo

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55