MSSQL奖章获得双打脑筋急转弯奖

时间:2017-03-21 23:37:35

标签: sql-server

我需要一种方法来计算双打事件的奖牌,其数据看起来像这样。

registered|Event_Name|gender_name|Participation_Name

5|Badminton Doubles|Male|Doubles

1|Badminton Doubles|Male|Doubles

3|Bowling Doubles|Female|Doubles

问题是奖牌(金牌,银牌和铜牌被授予每个双打队伍)。

所以在羽毛球的第一排有5名参赛者,我应该有2枚金牌,2枚银牌和1枚铜牌。

对于第二排,我应该有1金,0银和0铜。

对于第三排,我应该有2枚金牌,1枚银牌和0枚铜牌。

我可以使用什么SQL魔法进行此计算?

2 个答案:

答案 0 :(得分:0)

我只使用了3个案例陈述,每个奖章一个。我不知道这是多么可扩展,但它应该适用于这个用例。

,case
    when registered >= 2 then 2
    else registered
    end as gold
,case
    when registered >= 4 then 2
    when registered <= 2 then 0
    else 1
    end as silver
,case
    when registered >= 6 then 2
    when registered <= 4 then 0
    else 1
    end as bronze

答案 1 :(得分:0)

感谢您对此安德鲁的帮助,

这让我想到了将每一行的这个case语句放到一个新的临时表中,然后将双打奖牌计数加在一起。

这是任何人遇到此问题的结果。

    --get our singles medal counts for singles which is very simple
        set @goldsingles = (select count(registered) from #registered where registered > 0 and Participation_Name = 'Singles')
        set @silversingles = (select count(registered) from #registered where registered > 1 and Participation_Name = 'Singles')
        set @bronzesingles = (select count(registered) from #registered where registered > 2 and Participation_Name = 'Singles')

        --select our doubles data into a new temp table so that we can 
        --get medal counts for each event and then sum those all up
        select * 
        into #registereddoubles
        from #registered 
        where Participation_Name = 'Doubles' or Participation_Name = 'Mixed Doubles'

        --add columns to store the medal count for each event based on doubles logic
        alter table #registereddoubles 
        add doublesgold int not null default(0), doublessilver int not null default(0), doublesbronze int not null default(0);

        --update our events for the doubles golds
        --we want to up the number of medals for a partner even if a participant doesn't have a partner assigned yet
        update #registereddoubles 
        set doublesgold = (case when registered >= 1 then 2 else 0 end)

        update #registereddoubles 
        set doublessilver = (case when registered >= 3 then 2 else 0 end)
        from #registereddoubles rd2

        update #registereddoubles 
        set doublesbronze = (case when registered >= 5 then 2 else 0 end)
        from #registereddoubles rd2

        set @golddoubles = (select sum(doublesgold) from #registereddoubles )
        set @silverdoubles = (select sum(doublessilver) from #registereddoubles )
        set @bronzedoubles = (select sum(doublesbronze) from #registereddoubles )

        select @gold = @goldsingles + @golddoubles
        select @silver = @silversingles + @silverdoubles
        select @bronze = @bronzesingles + @bronzedoubles

        --select * from #registereddoubles

        --select * from #registered
        drop table #registered
        drop table #registereddoubles 

现在我必须为团队运动做一个更复杂的场景,但是它会遵循同样的想法来创建一个临时表,其中每个团队事件的奖牌数量将保留。