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 
3
  • 1
    Are you using MySQL or Postgres? They are different databases. Commented Jul 31, 2019 at 5:00
  • sorry my mistake, i'm using postgres Commented Jul 31, 2019 at 5:05
  • Are you really still using Postgres 9.1? In that case, you should plan the upgrade to the current (supported) version now. Commented Jul 31, 2019 at 6:41

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

the compiler says "the function string_agg() doesn't exist"
@RezaPratama yes, casting to varchar needed.
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; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.