PL / SQL 来自字符串的字数
PL / SQL word count from string
你能帮我计算字符串中的字数吗?格式如下:
n_var VARCHAR2(1000) := 'Hello, I like ham pizza more than mozzarella pizza.'
我需要这样的输出:hello => 1, I => 1, like => 1, ham => 1, pizza => 2...
我的想法是使用关联数组,但我不知道该怎么做。谢谢。
这是一种解决方案。它部署正则表达式将字符串中的单词拆分为标记。然后使用简单的聚合函数对这些标记进行计数。所以,纯 SQL 不需要关联数组或任何其他 PL/SQL 集合。
with dat as (
select 'Hello, I like ham pizza more than mozzarella pizza.' as str from dual
) , tkns as (
select regexp_substr(str, '([[:alnum:]]+)', 1, level) as tkn
from dat
connect by level <= regexp_count(str, '[\., ]')
)
select tkn, count(*)
from tkns
where tkn is not null
group by tkn
order by tkn
/
毫无疑问,还有更优雅的正则表达式解决方案,但至少这个是可行的。
另一种选择是下面的查询:
select word, count(1) as repeating
from
(
with t(str) as
(
select 'Hello, I like ham pizza more than mozzarella pizza' from dual
)
select regexp_replace(regexp_substr(str, '[^\ ]+', 1, level),'[^a-zA-Z]','')
as word
from t
cross join dual
connect by level <= regexp_count(str, '[^\ ]+')
)
group by word
order by repeating desc, word;
WORD REPEATING
---------- ---------
pizza 2
ham 1
Hello 1
I 1
like 1
more 1
mozzarella 1
than 1
你能帮我计算字符串中的字数吗?格式如下:
n_var VARCHAR2(1000) := 'Hello, I like ham pizza more than mozzarella pizza.'
我需要这样的输出:hello => 1, I => 1, like => 1, ham => 1, pizza => 2...
我的想法是使用关联数组,但我不知道该怎么做。谢谢。
这是一种解决方案。它部署正则表达式将字符串中的单词拆分为标记。然后使用简单的聚合函数对这些标记进行计数。所以,纯 SQL 不需要关联数组或任何其他 PL/SQL 集合。
with dat as (
select 'Hello, I like ham pizza more than mozzarella pizza.' as str from dual
) , tkns as (
select regexp_substr(str, '([[:alnum:]]+)', 1, level) as tkn
from dat
connect by level <= regexp_count(str, '[\., ]')
)
select tkn, count(*)
from tkns
where tkn is not null
group by tkn
order by tkn
/
毫无疑问,还有更优雅的正则表达式解决方案,但至少这个是可行的。
另一种选择是下面的查询:
select word, count(1) as repeating
from
(
with t(str) as
(
select 'Hello, I like ham pizza more than mozzarella pizza' from dual
)
select regexp_replace(regexp_substr(str, '[^\ ]+', 1, level),'[^a-zA-Z]','')
as word
from t
cross join dual
connect by level <= regexp_count(str, '[^\ ]+')
)
group by word
order by repeating desc, word;
WORD REPEATING
---------- ---------
pizza 2
ham 1
Hello 1
I 1
like 1
more 1
mozzarella 1
than 1