Skip to content
Advertisement

SQL query GROUP BY groups

I have something like this:

id name totalAmount
1 name1 10
2 name1 20
3 name1 25
4 name2 5
5 name2 12

And need to looks like this:

id’s name totalAmount
1,2 name1 30
2,3 name1 45
1,3 name1 35
1,2,3 name1 55
4,5 name2 17

I’m using the STRING_AGG but don’t know how to separated in the first 3 id’s.

Advertisement

Answer

Here is a recursive version which can handle more than 3 ids for a name and returns all possible combinations. As Dai points out though, take care as the number of combinations quickly mushrooms. But if your real data is like your example (normally 2-3 ids per name) than it should be fine.

Worth noting that I did this for fun. Probably you would be best just storing the raw data and doing this kind of shenanigans in the application layer.

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