I would like to count the regex instances from a table. For example:
message state ================================ [foo] aaaa active [bar] aaaa idle [foo] bbbb idle [foo] cccc active [bar] dddd active [tar] eeee idle
What I would like to have is following:
messageType ocurrences ==================================== [foo] 3 [bar] 2 [tar] 1
Is there any way to do that? Any help would be greatly appreciated!
If you just want to count the first "word" in the message, then use substring_index()
:
select substring_index(message, ' ', 1) as messageType, count(*)
from table t
group by substring_index(message, ' ', 1)
order by count(*) desc;
EDIT:
You can do this in Postgres by looking for the first space:
select left(message, position(' ' in message) as messageType, count(*)
from table t
group by messageType
order by count(*) desc;
just like the response above but with Postgres version:
select regexp_matches(message, '\[.+\]') as messageType, count (*) from table1 group by regexp_matches(message, '\[.+\]') order by count (*) desc;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.