Skip to content
Advertisement

How to grouping postgresql when id is same into new add value

I have table like this:

enter image description here

And I would like to bring value item in one row when user_input_id and question_id is duplicate.

The result that I wish is this:

enter image description here

Can anyone tell me how to querying it?


Thank You

Advertisement

Answer

You can easily do taht with string_agg(value,’,’) in postgresql.

select user_input_id,question_id,        
string_agg(value,',') as other_names
from table_name
group by  user_input_id,question_id
order by user_input_id,question_id

Output:

enter image description here

You can also have array_aggr() in postgres:

select user_input_id,question_id,        
array_agg(value::text ) as other_names
from table_name
group by  user_input_id,question_id
order by user_input_id,question_id

Output:

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement