我想计算表中的正则表达式实例。例如:
message state ================================ [foo] aaaa active [bar] aaaa idle [foo] bbbb idle [foo] cccc active [bar] dddd active [tar] eeee idle
我想要的是:
messageType ocurrences ==================================== [foo] 3 [bar] 2 [tar] 1
有什么办法吗? 任何帮助将不胜感激!
最佳答案
如果你只想计算消息中的第一个“单词”,那么使用substring_index()
:
select substring_index(message, ' ', 1) as messageType, count(*)
from table t
group by substring_index(message, ' ', 1)
order by count(*) desc;
编辑:
您可以在 Postgres 中通过查找第一个空格来执行此操作:
select left(message, position(' ' in message) as messageType, count(*)
from table t
group by messageType
order by count(*) desc;
关于SQL 计数正则表达式匹配 (PostgreSQL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32182160/