Suppose I have a table with the format
user, code, source
with sample data:
p1, a-b-c-d, g1
p2, b-c-d, g1
p4, q-a-b-c-d, g2
p5, b-e-d, g3
p6, q-a-c-d, g2
p7, c-d, g3
p3, a-b-a-a-d-e, g2
p8, a-b-a-a-d-e, g2
I want to write a query where I select all users who have a letter 'b' in their code, and then group by the letter that immediately follows it.
For example the output on the above should be:
count, group
3,c
1,e
2,a
To be clear there will always be a letter that follows 'b' if it exists.
Right now my query looks like:
select count(a.user), (string_to_array(a.code,'-'))[2]
from [table] a
where a.code LIKE 'b'
group by 2
But clearly this doesn't work