gpt4 book ai didi

sql - 表 SQL 中列值的超集

转载 作者:行者123 更新时间:2023-12-02 09:05:46 25 4
gpt4 key购买 nike

如何从(小型,大约 1-7 行)SQL 表创建值组合的超集?

例如我有一张 table :

MY_PATTERNS
╔═════════╗
║ PATTERN ║
╠═════════╣
║ 30 ║
║ 60 ║
║ 90 ║
╚═════════╝

并且有兴趣找到它的 super 集(不顺序敏感):

LISTAGG_PERMUTATIONS
╔══════════════════════╗
║ PATTERN_COMBINATIONS ║
╠══════════════════════╣
║ 30 ║
║ 30,60 ║
║ 30,60,90 ║
║ 30,90 ║
║ 60, ║
║ 60,90 ║
║ 90 ║
╚══════════════════════╝

是否有 SQL 查询可以实现此结果?到目前为止我所有的努力都是徒劳的......

我正在运行 Oracle 12c

I don't plan on running this on a set greater than 6 or 7 so O(n!) performance is not a concern.

最佳答案

您可以为此使用递归 CTE:

with cte(pattern_combination, l) as (
select pattern as pattern_combination, pattern as l
from my_patterns
union all
select cte.pattern_combination || ',' || p.pattern, p.pattern
from cte join
my_patterns p
on p.pattern > cte.l
)
select pattern_combination
from cte

Here是一个数据库<> fiddle 。

关于sql - 表 SQL 中列值的超集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58795208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com