【问题标题】:Generating all possible combinations of number set oracle sql生成数字集oracle sql的所有可能组合
【发布时间】:2021-02-03 08:34:35
【问题描述】:

在这个解决方案中 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

【问题讨论】:

  • 您是否需要一个像表值函数一样的参数化查询?

标签: sql oracle combinations


【解决方案1】:

我不确定我是否完全理解您的需求;如果是这样,这可能是一种方式:

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 &lt;=2 而不是 level &lt;= 26)并且我想获得最多 3 个字符的组合(level &lt;= 3),我会得到:

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

基本上,这不仅使用递归来生成起始字符集,甚至生成字符串,参数长度由查询获得的时间 (level) 对字符集进行递归

【讨论】:

    猜你喜欢
    • 2021-01-14
    • 2022-01-18
    • 1970-01-01
    • 2016-08-16
    • 2016-01-07
    相关资源
    最近更新 更多