I want to group all entries in my table by their user_type property.
However the user_type entries exist in a {type of user} {date account was created} format, e.g.
id | user_type
--------------
01 | 'admin 2024-03-01'
02 | 'admin 2024-03-01'
03 | 'user 2024-03-02'
04 | 'user 2024-03-02'
05 | 'premium_user 2024-03-03'
I want to get the counts of each type of user, ignoring the date at the end of the value. So for the above example, my query would return:
user_type | count
----------------------
'admin' | 2
'user' | 2
'premium_user' | 1
The user types are subject to change, so I can't hardcode in filters such as select count(id) where user_type like 'admin %'.
We can use
SUBSTRING()with a regular expression here:Here is a working SQL fiddle.