GROUPING 多个 LIKE 字符串
GROUPING multiple LIKE string
数据:
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种字符串我比较感兴趣warning, error 和timeout
我们可以有一个查询,它将按字符串分组并给出出现次数如下
输出:
timeout 3
warning 3
error 2
我知道我可以编写单独的查询来单独查找计数。但对单个查询感兴趣
谢谢
您可以为此使用过滤聚合:
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;
这 returns 三列计数,而不是您指定的三行计数。
如果您确实需要在单独的行中使用它,您可以使用 regexp_replace()
来清理字符串,然后按其分组:
select regexp_replace(the_column, '(.*)(warning|error|timeout)(.*)', '') as what,
count(*)
from the_table
group by what;
请使用以下查询,不要使用 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
演示:
如果你想在单独的行上使用它,你也可以使用横向连接:
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;
Here 是一个 db<>fiddle.
数据:
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种字符串我比较感兴趣warning, error 和timeout 我们可以有一个查询,它将按字符串分组并给出出现次数如下
输出:
timeout 3
warning 3
error 2
我知道我可以编写单独的查询来单独查找计数。但对单个查询感兴趣 谢谢
您可以为此使用过滤聚合:
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;
这 returns 三列计数,而不是您指定的三行计数。
如果您确实需要在单独的行中使用它,您可以使用 regexp_replace()
来清理字符串,然后按其分组:
select regexp_replace(the_column, '(.*)(warning|error|timeout)(.*)', '') as what,
count(*)
from the_table
group by what;
请使用以下查询,不要使用 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
演示:
如果你想在单独的行上使用它,你也可以使用横向连接:
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;
Here 是一个 db<>fiddle.