|
最闪亮滴星 发表于 2023-3-10 16:00
测试表create table testpath2 as (select '/data/'as datas ,'file1' as datapath ,'9999' as bytes from ...
If it can be an imperfect grouping for real request. My favor is (do not have link at hand) as below. Determine how many groups needed. Order all values in sequence. Pick biggest n (number of groups) values and put into each group first. Then put each following value into the group with minimum sum value at each step until all values are processed. This approach is quite efficient for data distribution with a few large bills and many coins (or simlar to filling tanks with a few rocks and much sand).
Quickly write one below to demonstrate it.
SQL> /
Enter value for gno: 5
Enter value for rowcnt: 10
old 1: with t0 as (select &gno gno, &rowcnt rowcnt from dual
new 1: with t0 as (select 5 gno, 10 rowcnt from dual
PATH_BY_ID GROUP_VALUE
-------------------------------------------------- -----------
1,10 11
5,6 11
2,9 11
4,7 11
3,8 11
SQL> /
Enter value for gno: 5
Enter value for rowcnt: 20
old 1: with t0 as (select &gno gno, &rowcnt rowcnt from dual
new 1: with t0 as (select 5 gno, 20 rowcnt from dual
PATH_BY_ID GROUP_VALUE
-------------------------------------------------- -----------
3,8,11,20 42
4,7,15,16 42
2,9,12,19 42
1,10,14,17 42
5,6,13,18 42
SQL>
SQL>
SQL> /
Enter value for gno: 5
Enter value for rowcnt: 40
old 1: with t0 as (select &gno gno, &rowcnt rowcnt from dual
new 1: with t0 as (select 5 gno, 40 rowcnt from dual
PATH_BY_ID GROUP_VALUE
-------------------------------------------------- -----------
4,7,15,16,23,28,31,40 164
3,8,11,20,24,27,35,36 164
2,9,12,19,22,29,32,39 164
5,6,13,18,21,30,34,37 164
1,10,14,17,25,26,33,38 164
SQL>
SQL>
==========================================================
with t0 as (select &gno gno, &rowcnt rowcnt from dual
), t as (select rownum val from t0 connect by rownum<=rowcnt
), tt as (select val, row_number() over (order by val desc) id from t
), tmp (rn, path, vals, lrn) as (
select 1, to_char(id), val, id from tt, t0 where id <=t0.gno
union all
select rn+1, path||decode(id, null, null, ','||id), vals+nvl(val, 0),
row_number() over (order by vals+nvl(val, 0) desc)
from tmp a, tt, t0
where rn+decode(lrn, gno, lrn) = tt.id (+)
and rn<=rowcnt-gno
)
select path path_by_id, vals group_value from tmp, t0
where rn=rowcnt-gno+1
/
|
|