SQL> with
2 -- sample data
3 test (datum, total, col) as
4 (select date '2020-07-20', 100, '10,0,20,30,0' from dual union all
5 select date '2020-07-20', 150, '15,3,40,30,2' from dual union all
6 --
7 select date '2020-07-19', 200, '50,6,50,30,8' from dual union all
8 select date '2020-07-19', 300, '20,1,40,10,2' from dual
9 ),
将csv值拆分为行。注意 RB 有助于我们求和匹配值的值
10 -- split comma-separated values into rows
11 temp as
12 (select
13 datum,
14 total,
15 to_number(regexp_substr(col, '\d+', 1, column_value)) val,
16 column_value rb
17 from test cross join
18 table(cast(multiset(select level from dual
19 connect by level <= regexp_count(col, ',') + 1
20 ) as sys.odcinumberlist))
21 ),
计算摘要简单;没什么特别的。我们将保留 RB 在最后一步中需要的值:
22 -- compute summaries
23 summary as
24 (select datum,
25 sum(total) total,
26 sum(val) sumval,
27 rb
28 from temp
29 group by datum, rb
30 )
最后一步。使用 LISTAGG ,将逗号分隔的值聚合回,但这次将彼此相加:
31 -- final result
32 select datum,
33 total,
34 listagg(sumval, ',') within group (order by rb) new_col
35 from summary
36 group by datum, total
37 order by datum desc, total;
DATUM TOTAL NEW_COL
------------------- ---------- --------------------
20.07.2020 00:00:00 250 25,3,60,60,2
19.07.2020 00:00:00 500 70,7,90,40,10
SQL>
1条答案
按热度按时间rjjhvcjd1#
这里有一个选择;阅读代码中的注解。我不想打字太多,所以要两次约会。
示例数据(您已经有了&不要键入它。您需要的代码从第10行开始:
将csv值拆分为行。注意
RB
有助于我们求和匹配值的值计算摘要简单;没什么特别的。我们将保留
RB
在最后一步中需要的值:最后一步。使用
LISTAGG
,将逗号分隔的值聚合回,但这次将彼此相加: