【问题标题】:Oracle: how to get unique combinations of 2 setsOracle:如何获得 2 组的唯一组合
【发布时间】:2021-01-05 11:08:43
【问题描述】:

样本数据:

create table group_ (
group_id number);

create table set_(
set_id number);

insert into group_ values(1);
insert into set_ values(1);
insert into set_ values(2);
insert into set_ values(3);

数据看起来像 组:1 设置:1,2,3

我想得到这 2 个表的所有组合,但 UNIQUE(1,2,3 与 1,3,2 相同),所以输出将是这样的:

    1
    1-1
    1-2
    1-3
    1-12
    1-13
    1-23
    1-123

我尝试使用 connect_by_path,但它得到了我或相同的组合(如 1-132)或缺少某些组合。 有什么想法吗?

【问题讨论】:

    标签: sql oracle plsql combinations


    【解决方案1】:

    假设您在 set_ 中没有重复值,并且您在预期结果中错过了 1-23,这可能是一种方法:

    select to_char(group_id)
    from group_
    UNION ALL
    select to_char(group_id) || '-' || replace (sys_connect_by_path(to_char(set_id), ' '), ' ', '')
    from set_
        cross join group_
    connect by prior set_id < set_id
    

    【讨论】:

    • 我当然错过了一种组合。非常感谢! :)
    • 1 个问题 - 3 组和 2 组 iget 几个重复的组合。为什么?
    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多