Home > Blockchain >  Group and Combine Rows In SQL
Group and Combine Rows In SQL

Time:10-29

I have a table that I unfortunately can't alter, but I can use to create a view. The issue is that the data in one column is spread across multiple rows. Here's a sample of what that looks like:

Customer Activity Note Sequence
1 Note The custo 1
1 Note mer calle 2
1 Note d and lef 3
1 Note t a messa 4
1 Note ge. 5
1 Charge $39.95 6
2 Charge $14.47 7

I need the data to look like this:

Customer Activity Note
1 Note The customer called and left a message.
1 Charge $39.95
2 Charge $14.47

Any ideas on how to do it?

CodePudding user response:

select   customer
        ,Activity
        ,string_agg(note, '') within group (order by Sequence) as note
from     t
group by customer, Activity
customer Activity note
1 Charge $39.95
2 Charge $14.47
1 Note The customer called and left a message

Fiddle

CodePudding user response:

try this: STRING_AGG Search for the syntax dialect according to the software you are using.

  • Related