zoukankan      html  css  js  c++  java
  • LeetCode 【困难】数据库-第1194:锦标赛优胜者

    题目

    数据

    结果

    解答

    1.因为分了两场,所以把两场连接为一场

    select first_player player_id,first_score score from matches #
    union all
    select second_player player_id,second_score score from matches
    

    2. 连接players表,按组别和玩家 分组计算得分

    select group_id,players.player_id,sum(score) score1 
    from players
    right join(
                select first_player player_id,first_score score from matches #
                union 
                select second_player player_id,second_score score from matches
               ) a
    on a.player_id = players.player_id
    group by group_id,player_id
    order by group_id
    

    3.按照组别分组,按分数排名(排名的时候,按照分数和用户id排名)!!!!!!

    select group_id,player_id,score1,
           rank() over (partition by group_id order by score1 desc,player_id) rn 
    from(
            select group_id,players.player_id,sum(score) score1 
            from players
            right join(
                      select first_player player_id,first_score score from matches #
                      union 
                      select second_player player_id,second_score score from matches
                       ) a
            on a.player_id = players.player_id
            group by group_id,player_id
            order by group_id
        ) b 
    

    4.筛选出来第一名的人,(按照分数和id)

    select group_id,player_id ,rn
    from(
        select group_id,player_id,score1,
            rank() over (partition by group_id order by score1 desc,player_id) rn 
        from(
            select group_id,players.player_id,sum(score) score1 
            from players
            right join(
                select first_player player_id,first_score score from matches #
                union 
                select second_player player_id,second_score score from matches
            ) a
            on a.player_id = players.player_id
            group by group_id,player_id
    				order by group_id
        )b 
    )c
    where rn = 1
    

  • 相关阅读:
    jq clone 克隆方法偶遇问题
    截取字符串加省略号
    瀑布流页面效果
    js 函数节流 jQuery throttle/debounce
    手机浏览页面touch操作,解决touch事件和浏览器滚动条冲突问题;zepto,swipe事件。
    touch 方向判断方法
    thinkPHP 导入栗子 (2)
    百度地图的使用实例-php+jq
    mac上配置react-native环境run-ios/run-android命令遇到的问题
    npm、cnpm、yarn 安装删除异同
  • 原文地址:https://www.cnblogs.com/Tdazheng/p/14981005.html
Copyright © 2011-2022 走看看