我給你的方法:
- 除以列值會給行數您需要拆分。
- 然後進一步除以該數量將會把它分成相同的桶數。然後剩下的就是行發生器的方法。
測試用例:
SQL> CREATE TABLE t(
2 topcode VARCHAR2(10),
3 totalpeep NUMBER);
Table created.
SQL>
SQL> INSERT INTO t VALUES('A', 100);
1 row created.
SQL> INSERT INTO t VALUES('B', 45);
1 row created.
SQL>
SQL> SELECT * FROM t;
TOPCODE TOTALPEEP
---------- ----------
A 100
B 45
SQL>
因此,SQL創建基於計數桶的數量相等:
SQL> WITH DATA AS
2 (SELECT t.*, t.totalpeep/20 wt_bucket FROM t)
3 SELECT topcode, topcode||level
4 FROM DATA
5 CONNECT BY LEVEL <= wt_bucket
6 AND PRIOR topcode = topcode
7 AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL
8/
TOPCODE TOPCODE||LEVEL
---------- --------------------------------------------
A A1
A A2
A A3
A A4
A A5
B B1
B B2
7 rows selected.
SQL>
即使Totalpeep小於20那麼這必須有一組
在這種情況下,未使用的剩餘桶,也應考慮:
SQL> WITH DATA AS
2 (SELECT t.*, t.totalpeep/20 wt_bucket FROM t)
3 SELECT topcode, topcode||level
4 FROM DATA
5 CONNECT BY LEVEL <= ceil(wt_bucket)
6 AND PRIOR topcode = topcode
7 AND PRIOR DBMS_RANDOM.VALUE IS NOT NULL
8/
TOPCODE TOPCODE||LEVEL
---------- --------------------------------------------
A A1
A A2
A A3
A A4
A A5
B B1
B B2
B B3
8 rows selected.
SQL>
因此,與值組沒有劃分後一個整數,你可以考慮他們。
有層次和排輩的組合在這裏。看到我的答案。 – 2015-03-03 12:05:48