Вы можете использовать функцию aggregate
с xmlquery
:
Создание таблицы:
SQL> CREATE TABLE RR_SRC_LEK_P5 AS (
2 SELECT '00004146' AS C216, '4523401' AS LIST_OF_PARENT_CCN, 0 AS ESS_SUM FROM DUAL UNION ALL
3 SELECT '00005354', '5487228, 2103188', 0 FROM DUAL UNION ALL
4 SELECT '00006203', NULL ,0 FROM DUAL UNION ALL
5 SELECT '00007524', '2196702, 2103188', 0 FROM DUAL UNION ALL
6 SELECT '00009164', '2163921, 4523401', 0 FROM DUAL);
Table created.
SQL> CREATE TABLE RR_SRC_LEK_P6 AS
2 (SELECT 4523401 AS PARENT_CCN, 2584186507711 AS ESS_SUM FROM DUAL UNION ALL
3 SELECT 5487228, 253404419664 FROM DUAL UNION ALL
4 SELECT 2103188, 134827833560255 FROM DUAL UNION ALL
5 SELECT 2196702, 993687558 FROM DUAL UNION ALL
6 SELECT 2163921, 1413297813349 FROM DUAL);
Table created.
SQL>
Просмотр текущих данных
SQL> select * from RR_SRC_LEK_P5;
C216 LIST_OF_PARENT_C ESS_SUM
-------- ---------------- ----------
00004146 4523401 0
00005354 5487228, 2103188 0
00006203 0
00007524 2196702, 2103188 0
00009164 2163921, 4523401 0
SQL> select * from RR_SRC_LEK_P6;
PARENT_CCN ESS_SUM
---------- ---------------------------------------
4523401 2584186507711
5487228 253404419664
2103188 134827833560255
2196702 993687558
2163921 1413297813349
SQL>
Запрос на обновление данных
SQL>
SQL> MERGE INTO RR_SRC_LEK_P5 P5
2 USING ( SELECT P5.C216,
3 XMLQUERY(
4 LISTAGG(P6.ESS_SUM, '+') WITHIN GROUP (ORDER BY NULL)
5 RETURNING CONTENT ).GETNUMBERVAL() AS ESS_NEW
6 FROM RR_SRC_LEK_P5 P5
7 JOIN RR_SRC_LEK_P6 P6
8 ON ( INSTR(', ' || P5.LIST_OF_PARENT_CCN || ',',
9 ', ' || P6.PARENT_CCN || ',') > 0 )
10 GROUP BY P5.C216) RES
11 ON ( P5.C216 = RES.C216 )
12 WHEN MATCHED THEN
13 UPDATE SET P5.ESS_SUM = RES.ESS_NEW;
4 rows merged.
SQL>
Обновленные данные
SQL> SELECT * FROM RR_SRC_LEK_P5;
C216 LIST_OF_PARENT_C ESS_SUM
-------- ---------------- ---------------------------------------
00004146 4523401 2584186507711
00005354 5487228, 2103188 135081237979919
00006203 0
00007524 2196702, 2103188 134828827247813
00009164 2163921, 4523401 3997484321060
SQL>
DB <> Fiddle demo
Обновление
Для ваших настроек вам необходимо использовать следующий запрос:
MERGE INTO RR_SRC_LEK_P5 P5
USING ( SELECT P5.C216,
XMLQUERY(
LISTAGG(
to_char(p6.ESS_SUM,
'fm9999999999999999999999999999D099999999999999999',
'nls_numeric_characters = ''.,'''), '+') WITHIN GROUP (ORDER BY NULL)
RETURNING CONTENT ).GETNUMBERVAL() AS ESS_NEW
FROM RR_SRC_LEK_P5 P5
JOIN RR_SRC_LEK_P6 P6
ON ( INSTR(', ' || P5.LIST_OF_PARENT_CCN || ',',
', ' || P6.PARENT_CCN || ',') > 0 )
GROUP BY P5.C216) RES
ON ( P5.C216 = RES.C216 )
WHEN MATCHED THEN
UPDATE SET P5.ESS_SUM = RES.ESS_NEW;
Ура !!