簡體   English   中英

生成數字集的所有可能組合 oracle sql

[英]Generating all possible combinations of number set oracle sql

在這個解決方案中https://stackoverflow.com/a/33565783/9737797非常感謝https://stackoverflow.com/users/3989608/lalit-kumar-b Mr.Lalit Kumar B。 但是我必須使用另一種組合來連接其他長度的查詢命令。 有沒有可能,我怎樣才能動態設置這個組合長度? 提前謝謝。 此致。

查詢是:

WITH combinations AS
   (SELECT chr( ascii('A')+level-1 ) c FROM dual CONNECT BY level <= 26)
  SELECT * FROM combinations
    UNION ALL
  SELECT c1.c || c2.c FROM combinations c1, combinations c2
    UNION ALL SELECT c1.c || c2.c || c3.c FROM combinations c1, combinations c2, combinations c3;

結果(長度二):00 01 11 10

其他組合結果(長度三):000 001 010 011 100 101 110 111

我不確定我是否完全理解您的需求; 如果是這樣,這可能是一種方式:

with characters(c) as
(
     select chr( ascii('A') + level -1)
     from dual
     connect by level <= 2                      /* 2 instead of 26, just to try it */
)
select replace (sys_connect_by_path(c, ' '), ' ', '') as result
from characters
connect by level <= 3                           /* the length you need  */
order by level, result

例如,如果我只使用ABlevel <=2而不是level <= 26 )並且我想獲得最多 3 個字符的組合( level <= 3 ),我得到:

A
B
AA
AB
BA
BB
AAA
AAB
ABA
ABB
BAA
BAB
BBA
BBB

基本上,這不僅使用遞歸生成起始字符集,甚至生成字符串,其參數長度由時間( level )獲得,查詢對字符集進行遞歸

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM