grouping multiple-like字符串

k5hmc34c  于 2021-07-26  发布在  Java
关注(0)|答案(3)|浏览(601)

数据:

2015478 warning occurred at 20201403021545
2020179 error occurred at 20201303021545
2025480 timeout occurred at 20201203021545
2025481 timeout occurred at 20201103021545
2020482 error occurred at 20201473021545
2020157 timeout occurred at 20201403781545
2020154 warning occurred at 20201407851545
2027845 warning occurred at 20201403458745

在上面的数据中,我感兴趣的字符串有3种:警告、错误和超时。我们可以用一个单独的查询,它将按字符串分组,并给出出现次数,如下所示
输出:

timeout 3
warning 3
error 2

我知道我可以编写单独的查询来逐个查找count。但对一个问题感兴趣谢谢

tf7tbtn2

tf7tbtn21#

您可以使用筛选聚合:

select count(*) filter (where the_column like '%timeout%') as timeout_count, 
       count(*) filter (where the_column like '%error%') as error_count, 
       count(*) filter (where the_column like '%warning%') as warning_count
from the_table;

这将返回三列中的计数,而不是所示的三行。
如果您确实需要在单独的行中使用它,您可以使用 regexp_replace() 要清除字符串,请按以下方式分组:

select regexp_replace(the_column, '(.*)(warning|error|timeout)(.*)', '\2') as what,
       count(*) 
from the_table
group by what;
qeeaahzv

qeeaahzv2#

请使用下面的查询,没有硬编码的值使用 STRPOS ```
select val, count(1) from
(select substring(column_name ,position(' ' in (column_name))+1,
length(column_name) - position(reverse(' ') in reverse(column_name)) -
position(' ' in (column_name))) as val from matching) qry
group by val; -- Provide the proper column name

演示:
![](https://i.stack.imgur.com/CABlG.png)
ddhy6vgd

ddhy6vgd3#

如果要在单独的行上执行此操作,也可以使用横向联接:

select which, count(*)
from t cross join lateral
     (values (case when col like '%error%' then 'error' end),
             (case when col like '%warning%' then 'warning' end),
             (case when col like '%timeout%' then 'timeout' end)
     ) v(which)
where which is not null
group by which;

另一方面,如果您只需要第二个单词,但不想硬编码值,则可以使用:

select split_part(col, ' ', 2) as which, count(*)
from t
group by which;

这是一把小提琴。

相关问题