代码之家  ›  专栏  ›  技术社区  ›  Ychdziu

组合数学在oraclesql中的应用

  •  1
  • Ychdziu  · 技术社区  · 8 年前

    可能需要一些帮助或见解,因为我快疯了。。

    • 形势
    • 目标 :想要从可用的球员中创建一个4名球员的花名册(在我们的例子中有7名球员)。这是一个经典的组合任务,我们需要计算C(k,n)。在我们的例子中,C(4,7)=840/24=35。所以,有可能有35种方法来建立一个名册。我想创建一个带有玩家ID的花名册表。以下是当前脚本,用于构建当前花名册表:

    with comb_tbl as( select tmp_out.row_num, regexp_substr(tmp_out.comb_sets,'[^,]+',1,1) plr_id_1, regexp_substr(tmp_out.comb_sets,'[^,]+',1,2) plr_id_2, regexp_substr(tmp_out.comb_sets,'[^,]+',1,3) plr_id_3, regexp_substr(tmp_out.comb_sets,'[^,]+',1,4) plr_id_4 from( select rownum row_num, substr(tmp.combinations,2) comb_sets from( select sys_connect_by_path(plr.plr_id, ',') combinations from( select 1 plr_id from dual union select 2 plr_id from dual union select 3 plr_id from dual union select 4 plr_id from dual union select 5 plr_id from dual union select 6 plr_id from dual union select 7 plr_id from dual) plr connect by nocycle prior plr.plr_id != plr.plr_id) tmp where length(substr(tmp.combinations,2)) = 7) tmp_out) select tmp1.* from comb_tbl tmp1

    • 问题 :它创建840个可能性,但我需要删除“相同”的可能性,例如,花名册(1,2,3,4)与花名册(2,1,3,4)是“相同”的。欢迎任何见解/评论/批评。也许这种方法本身就错了?
    3 回复  |  直到 8 年前
        1
  •  3
  •   mathguy    8 年前

    在可能的名册和七要素集中四个要素的有序子集之间有1-1的对应关系。

    在你的 CONNECT BY != 到 < 在里面 连接方式 它会起作用的。(另外,您不需要 NOCYCLE 再也没有了。)

        2
  •  2
  •   Aleksej    8 年前

    这可能是一种连接方式:

    with plr(plr_id) as 
        ( select level from dual connect by level <= 7)
    select p1.plr_id, p2.plr_id, p3.plr_id, p4.plr_id
    from plr p1
          inner join plr p2
            on(p1.plr_id < p2.plr_id)  
          inner join plr p3
            on(p2.plr_id < p3.plr_id)
          inner join plr p4
            on(p3.plr_id < p4.plr_id) 
    

    例如,使用 n=5

        PLR_ID     PLR_ID     PLR_ID     PLR_ID
    ---------- ---------- ---------- ----------
             1          2          3          4
             1          2          3          5
             1          2          4          5
             1          3          4          5
             2          3          4          5
    
        3
  •  1
  •   Boneist    8 年前

    这不是答案,只是我先前评论的延续:

    通过进行这些额外的更改,您的查询可能会以如下方式结束:

    SELECT regexp_substr(tmp_out.comb_sets,'[^,]+',1,1) plr_id_1,
           regexp_substr(tmp_out.comb_sets,'[^,]+',1,2) plr_id_2,
           regexp_substr(tmp_out.comb_sets,'[^,]+',1,3) plr_id_3,
           regexp_substr(tmp_out.comb_sets,'[^,]+',1,4) plr_id_4
    FROM   (SELECT sys_connect_by_path(plr.plr_id, ',') comb_sets,
                   LEVEL lvl
            FROM   (select 1 plr_id from dual union all
                    select 2 plr_id from dual union all
                    select 3 plr_id from dual union all
                    select 4 plr_id from dual union all
                    select 5 plr_id from dual union all
                    select 6 plr_id from dual union all
                    select 7 plr_id from dual) plr
            CONNECT BY PRIOR plr.plr_id < plr.plr_id
                       AND LEVEL <= 4) tmp_out
    WHERE lvl = 4;