sql—查找两列的唯一组合,其中每个值只出现一次

3okqufwl  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(336)

我试着建立一个游戏对决,每个球员只玩一次,最大限度地提高游戏总数。在15种可能的对决中,我试图找出p1和p2的组合。
我尝试过的代码:尝试1

with cte1 as(
select p1,p2,ROW_NUMBER() over(partition by p1 order by (select 0) desc) rn 
from #tempgame
) ,cte2 as (select p1,p2,ROW_NUMBER() over(partition by p2 order by (select 0) desc) rn 
from cte1
where rn=1
)select * from cte2
where rn = 1

尝试2:

select distinct a.p2,b.p1
    from #tempgame a
    join #tempgame b
    on a.p1 = 
            ( select top 1 a.p1
            from #tempgame b
            where b.p2 = a.p2

            )

我甚至还没有达到每个球员只打一次球的第一个条件,更不用说最大限度地提高比赛的数量了。
我已经根据玩家的可用性决定了只有这些对决是可能的,并利用了他们可用性的中段来决定游戏时间。所以我不能只是把玩家表随机分配给他们,如果他们的可用时间不重叠的话。所以问题很简单:基于简单的p1,p2表,允许什么p1,p2组合?

select * 
INTO #tempgame
from (
select  76561197987822470 p1,76561198040907827 p2 union all 
select 76561197987822470,76561198088336999 union all
select 76561197987822470,76561198095503172 union all
select 76561197987822470,76561198397303730 union all
select 76561198001599297,76561198397303730 union all
select 76561198001599297,76561198321977951 union all
select 76561198001599297,76561198095503172 union all
select 76561198040907827,76561198088336999 union all
select 76561198040907827,76561198095503172 union all
select 76561198040907827,76561198397303730 union all
select 76561198088336999,76561198397303730 union all
select 76561198088336999,76561198095503172 union all
select 76561198095503172,76561198397303730 union all
select 76561198095503172,76561198321977951 union all
select 76561198321977951,76561198397303730 
) z

我相信只有2场对决是可能的。我不管是哪一个。但除了在小样本上反复试验之外,我找不到一个合理的计算方法。

P1                           P2
row1 76561197987822470 vs 76561198040907827
row2 76561198001599297 vs 76561198397303730
yftpprvb

yftpprvb1#

我试着建立一个游戏对决,每个球员只玩一次,最大限度地提高游戏总数。
如果这是您的目标,请从player表开始并使用自连接:

select p1.player_id, p2.player_id
from players p1 join
     players p2
     on p1.player_id < p2.player_id

我不知道是什么 gametime 与问题有关——或者实际上,数据或查询与问题有什么关系。

相关问题