甲骨文 10g
我的要求是:
- 选择每个部门
- 选择每个部门的每个单独项目(每个项目都有自己的行,但如果重复则合并)
- 选择每个不同部门和项目的每种颜色(如果重复,请选择最小的数字)
- 选择每个不同部门和项目的每个用户(如果有多个则聚合)
数据库数据
Department Item_List Color User
Research Item 1 1. Blue John
Research Item 1;Item 2 2. Blue Mike
Research Item 1;Item 2; Item 3 1. Red Steve
Research Item 2 1. Purple John
Research Item 1;Item 4 2. Red Bill
Ops Item 1;Item 2 3. Silver John
Ops Item 1;Item 3 3. Silver Mike
Ops Item 4 4. Yellow Mark
预期成绩
Department Item_List Color User
Research Item 1 1. Blue John, Mike
Research Item 2 1. Blue Mike
Research Item 1 1. Red Steve, Bill
Research Item 2 1. Red Steve
Research Item 3 1. Red Steve
Research Item 2 1. Purple John
Research Item 4 1. Red Bill
Ops Item 1 3. Silver John, Mike
Ops Item 2 3. Silver John
Ops Item 3 3 Silver Mike
Ops Item 4 4. Yellow Mark
我正在使用以下 SQL,但它不起作用:
with data as
(
select
DEPARTMENT,
ITEM_LIST,
(length(ITEM_LIST)-length(replace(ITEM_LIST,';','')))+1 cnt,
MIN(Color) as Color,
wm_concat(USER) as USER
from DataBase_Table
Group by
DEPARTMENT,
ITEM_LIST
)
select
DEPARTMENT
ITEM_LIST,
Color,
User
from
(
select distinct
DEPARTMENT,
ltrim(regexp_substr(ITEM_LIST,'[^;]+',1,level)) ITEM_LIST,
Color,
level,
User
from data
connect by level <= cnt
order by DEPARTMENT
)
;