WITH t AS
(SELECT '1,2,3,4' a, 1 b
FROM Dual
UNION ALL
SELECT '1,2,3' a, 2 b FROM Dual),
p AS
(SELECT 1 c, 'A' d
FROM Dual
UNION ALL
SELECT 2 c, 'B'
FROM Dual
UNION ALL
SELECT 3 c, 'C'
FROM Dual
UNION ALL
SELECT 4 c, 'D' FROM Dual)
SELECT t.b, Listagg(d, ',') Within GROUP(ORDER BY d)
FROM p,
(SELECT b, Regexp_Substr(a, '[^,]+', 1, LEVEL) i
FROM t
CONNECT BY LEVEL <= Regexp_Count(a, ',') + 1
GROUP BY b, a, LEVEL) t
WHERE t.i = p.c
GROUP BY t.b
(SELECT '1,2,3,4' a, 1 b
FROM Dual
UNION ALL
SELECT '1,2,3' a, 2 b FROM Dual),
p AS
(SELECT 1 c, 'A' d
FROM Dual
UNION ALL
SELECT 2 c, 'B'
FROM Dual
UNION ALL
SELECT 3 c, 'C'
FROM Dual
UNION ALL
SELECT 4 c, 'D' FROM Dual)
SELECT t.b, Listagg(d, ',') Within GROUP(ORDER BY d)
FROM p,
(SELECT b, Regexp_Substr(a, '[^,]+', 1, LEVEL) i
FROM t
CONNECT BY LEVEL <= Regexp_Count(a, ',') + 1
GROUP BY b, a, LEVEL) t
WHERE t.i = p.c
GROUP BY t.b