来自字符串的PL / SQL字数统计

时间:2019-02-05 16:47:44

标签: sql oracle associative-array

可以帮我计算字符串中的单词数吗?格式如下:

n_var VARCHAR2(1000) := 'Hello, I like ham pizza more than mozzarella pizza.'

我需要输出以下内容:hello => 1,I => 1,like => 1,火腿=> 1,比萨=> 2 ...

我的想法是使用关联数组,但是我不确定该怎么做。谢谢。

2 个答案:

答案 0 :(得分:0)

这是一种解决方案。它部署正则表达式将单词从字符串中拆分为标记。然后使用简单的聚合函数对这些令牌进行计数。因此,纯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
/ 

毫无疑问,有更多优雅的正则表达式解决方案,但至少这一项有效。

答案 1 :(得分:0)

另一个选择是下面的查询:

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