SELECT
c."Key",
LISTAGG(s.Name, ',') WITHIN GROUP (ORDER BY s.Dept) Departments
FROM ConfigDB c
LEFT JOIN StaffDB s
ON ',' || c."Value" || ',' LIKE '%,' || s.Dept || '%,'
GROUP BY
c."Key";
一种选择是用逗号分开,然后用 LISTAGG() 内部功能 SELECT .. CONNECT BY level .. 声明:
WITH ConfigDB( key, value ) AS
(
SELECT 'dept', 'A,B,C' FROM dual
), StaffDB( name, dept ) AS
(
SELECT 'tom' , 'A' FROM dual UNION ALL
SELECT 'ken' , 'B' FROM dual UNION ALL
SELECT 'jerry', 'C' FROM dual
), t AS
(
SELECT REGEXP_SUBSTR(value,'[^,]',1,level) AS letter, level AS lvl
FROM ConfigDB
CONNECT BY level <= REGEXP_COUNT(value,',') + 1
)
SELECT LISTAGG(name,',') WITHIN GROUP (ORDER BY lvl) AS DBNAme
FROM t
JOIN StaffDB
ON dept = letter
select * from
(select LISTAGG(Name, ',') WITHIN GROUP (ORDER BY Dept) as name,
LISTAGG(Dept, ',') WITHIN GROUP (ORDER BY Dept) as dept from staffDB)
where dept in
(select value from ConfigDB);
4条答案
按热度按时间eivgtgni1#
您的数据库设计不是最佳的,因为它存储的是csv值。也就是说,这里有一种方法可以获得您想要使用的输出
LISTAGG
(11g+):演示
请注意,我们使用以下逻辑将config表中的部门csv列表与staff表中的每个标量值进行比较,例如:
也就是说,我们在部门的csv字符串上加上逗号,这样我们就可以搜索每个可能的标量部门。
bsxbgnwa2#
一种选择是用逗号分开,然后用
LISTAGG()
内部功能SELECT .. CONNECT BY level ..
声明:演示
uoifb46i3#
可以使用层次结构查询拆分值,然后按如下方式将其联接:
rkttyhzu4#
请使用下面的查询,
下面是测试用例,