需要帮助创建一个SQL语句

问题描述 投票:0回答:2

下面是表数据

Name       Stage ID       Event ID     Attempt No            Score
Ramesh        1             1             1                    10
Ramesh        1             1             2                    20
Ramesh        2             1             1                    30
Suresh        1             1             1                    05
Suresh        2             1             1                    15
Suresh        2             2             1                    25
Suresh        2             2             2                    35
Suresh        2             2             3                    30

我们有一个表名,舞台ID,事件ID,尝试不,得分。

我们要组名的数据,关卡ID,事件ID,尝试不,MAX(分数)

有可能是下阶段和事件,并将其命名千方百计记录,我们需要要显示的最高分和同样的需求。

输出应该是:

Name       Stage ID       Event ID     Attempt No            Score
Ramesh        1             1             2                    20
Ramesh        2             1             1                    30
Suresh        1             1             1                    05
Suresh        2             1             1                    15
Suresh        2             2             2                    35

这将有一个记录名称+级+事件组合

mysql sql
2个回答
1
投票

你可以试试下面利用相关子查询

select * from tablename t1
where score in 
    (select max(score) from tablename t2 where t1.name=t2.name and t1.stage=t2.stage 
      and t1.eventid=t2.eventid)

0
投票

你可以使用的子查询您需要最高值的内连接

select yt.* 
from your_table yt
INNER JOIN ( 
    select Name, Stage_ID, Event_ID, Attempt_No, max(score) max_score 
    from your_table 
    group by  Name,  Stage_ID, Event_ID, Attempt_No
) t on yt.name  = t.name 
      and yt.Stage_ID = t.Stage_ID
          and yt.Event_ID = t.Event_ID 
            and yt.Attempt_No = t.Attempt_No 
              and yt.score  = t.max_score   join 
© www.soinside.com 2019 - 2024. All rights reserved.