i’ve got a table that’s kinda like this
id | account |
---|---|
111111 | 333-333-2 |
111111 | 333-333-1 |
222222 | 444-444-1 |
222222 | 555-555-1 |
222222 | 555-555-2 |
and i’m trying to aggregate everything up to look like this
id | account |
---|---|
111111 | 333-333-1, -2 |
222222 | 444-444-1, 555-555-1, -2 |
so far i’ve got this
SELECT
id,
CONCAT((STRING_AGG(DISTINCT SUBSTRING(account FROM '^(([^-]*-){2})'), ', ')),
(STRING_AGG(DISTINCT SUBSTRING(account FROM '[^-]*$'), ', '))) account
GROUP BY id
but this produces
id | account |
---|---|
111111 | 333-333-1, 2 |
222222 | 444-444-, 555-555-1, 2 |
2
Answers
i ended up figuring it out and this worked for me :))
I would suggest a different approach: first split the account numbers into main part and suffix, then do separate grouping operations on them: