Oracle11g 计算一个字段结果中出现的字符串数-Oracle 11

Oracle11g 计算一个字段结果中出现的字符串数-Oracle 11,oracle11g,aggregate,Oracle11g,Aggregate,我想用这种方法计算字符串的出现次数: ID | NAME | ITEMS 1 | JFK | 100/100/100/200/300/300 我想把它变成 ID | NAME | ITEMS 1 | JFK | 100(3),200(1),300(2) 一个函数就可以了,但是如果你能在一个SELECT中完成,那就太棒了 我使用的是Oracle11,但如果你能提供通用的,可能会帮助更多的人 谢谢:)这有点冗长,但您可以将字符串标记为单独的行: select id, name, rege

我想用这种方法计算字符串的出现次数:

ID | NAME | ITEMS
1  | JFK  | 100/100/100/200/300/300
我想把它变成

ID | NAME | ITEMS
1  | JFK  | 100(3),200(1),300(2)
一个函数就可以了,但是如果你能在一个SELECT中完成,那就太棒了

我使用的是Oracle11,但如果你能提供通用的,可能会帮助更多的人


谢谢:)

这有点冗长,但您可以将字符串标记为单独的行:

select id, name, regexp_substr(items, '(.*?)(/|$)', 1, level, null, 1) as item
from your_table
connect by level < regexp_count(items, '(.*?)(/|$)')
and prior id = id
and prior dbms_random.value is not null;
这个问题得到了解决

        ID NAM ITEMS                                             
---------- --- --------------------------------------------------
         1 JFK 100(3)/200(1)/300(2)                              
         2 LBJ 100(3)/200(1)/300(1)                              

如果订购的不是那么整齐,您希望看到什么,例如
100/300/100/200/100
select id, name,
  listagg(item || '(' || item_count || ')', '/') within group (order by item) as items
from (
  select id, name, item, count(*) as item_count
  from (
    select id, name, regexp_substr(items, '(.*?)(/|$)', 1, level, null, 1) as item
    from your_table
    connect by level < regexp_count(items, '(.*?)(/|$)')
    and prior id = id
    and prior dbms_random.value is not null
  )
  group by id, name, item
)
group by id, name
order by id;
with your_table (id, name, items) as (
            select 1, 'JFK', '100/100/100/200/300/300' from dual
  union all select 2, 'LBJ', '100/300/100/200/100' from dual
)
select ...
        ID NAM ITEMS                                             
---------- --- --------------------------------------------------
         1 JFK 100(3)/200(1)/300(2)                              
         2 LBJ 100(3)/200(1)/300(1)