[英]SQL count regex matches (PostgreSQL)
我想从表中计算正则表达式实例。 例如:
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;
就像上面的响应一样,但带有Postgres版本:
select regexp_matches(message, '\[.+\]') as messageType, count (*) from table1 group by regexp_matches(message, '\[.+\]') order by count (*) desc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.