当前位置: 首页 > 面试题库 >

根据最大值联接表

丁志勇
2023-03-14
问题内容

这是我正在谈论的简化示例:

Table: students      exam_results
_____________       ____________________________________
| id | name |       | id | student_id | score |   date |
|----+------|       |----+------------+-------+--------|
|  1 | Jim  |       |  1 |          1 |    73 | 8/1/09 | 
|  2 | Joe  |       |  2 |          1 |    67 | 9/2/09 |
|  3 | Jay  |       |  3 |          1 |    93 | 1/3/09 |
|____|______|       |  4 |          2 |    27 | 4/9/09 |
                    |  5 |          2 |    17 | 8/9/09 |
                    |  6 |          3 |   100 | 1/6/09 |
                    |____|____________|_______|________|

为了这个问题,假设每个学生至少记录了一个考试结果。

您将如何选择每个学生以及他们的最高分数? 编辑 :…和该记录中的其他字段?

预期产量:

_________________________
| name | score |   date |
|------+-------|--------|
|  Jim |    93 | 1/3/09 |
|  Joe |    27 | 4/9/09 |
|  Jay |   100 | 1/6/09 |
|______|_______|________|

欢迎使用所有类型的DBMS进行回答。


问题答案:

回答已编辑的问题(即也获取关联的列)。

在Sql Server 2005+中,最好的方法是将等级/窗口函数与CTE结合使用,如下所示:

with exam_data as
(
    select  r.student_id, r.score, r.date,
            row_number() over(partition by r.student_id order by r.score desc) as rn
    from    exam_results r
)
select  s.name, d.score, d.date, d.student_id
from    students s
join    exam_data d
on      s.id = d.student_id
where   d.rn = 1;

对于符合ANSI-SQL的解决方案,子查询和自联接将起作用,如下所示:

select  s.name, r.student_id, r.score, r.date
from    (
            select  r.student_id, max(r.score) as max_score
            from    exam_results r
            group by r.student_id
        ) d
join    exam_results r
on      r.student_id = d.student_id
and     r.score = d.max_score
join    students s
on      s.id = r.student_id;

最后一个假设没有重复的student_id /
max_score组合,如果有和/或您打算对它们进行重复数据删除,则需要使用另一个子查询与确定性的内容一起加入,以确定要提取的记录。例如,假设您不能为同一日期的给定学生提供多个记录,如果您想基于最新的max_score打破平局,则可以执行以下操作:

select  s.name, r3.student_id, r3.score, r3.date, r3.other_column_a, ...
from    (
            select  r2.student_id, r2.score as max_score, max(r2.date) as max_score_max_date
            from    (
                        select  r1.student_id, max(r1.score) as max_score
                        from    exam_results r1
                        group by r1.student_id
                    ) d
            join    exam_results r2
            on      r2.student_id = d.student_id
            and     r2.score = d.max_score
            group by r2.student_id, r2.score
        ) r
join    exam_results r3
on      r3.student_id = r.student_id
and     r3.score = r.max_score
and     r3.date = r.max_score_max_date
join    students s
on      s.id = r3.student_id;

编辑:由于Mark在评论中的出色表现,添加了正确的重复数据删除查询



 类似资料:
  • 问题内容: 如何构造一个mySQL查询以基于最大值删除行。 我试过了 但是有错误? 问题答案: 采用: 请注意,如果存在重复,则将删除 所有 具有该值的行。 关于1093错误的愚蠢之处在于,您可以通过在自引用之间放置一个子查询来解决它: 说明 MySQL仅在使用&语句时检查是否存在正在更新的同一表的一级子查询。这就是为什么将其放在第二级(或更深层)的子查询替代项中的原因。但这只是检查子查询- JO

  • 问题内容: 我有三个mysql表,我想从中提取一些信息,这些表是: 视频-代表带有分数的视频。 标签-包含标签的全局列表。 VideoTags在视频和标签之间创建关联。 我想做的就是找到每个标签的得分最高的视频。有许多具有相同标签的视频,但是我的结果集将具有与标签相同的行数。最终目标是为每个唯一标签(标签是主题加上哈希值)提供最佳视频列表(按得分)。 我的SQL noob尝试实现此目标的方法如下:

  • 嗨,伙计们,我正在使用Laravel5.5,我有两个表,用户和服务 用户表 ID 名称 电子邮件 密码 地址 城市 国家 邮政编码 null 提前谢了。热烈问候:阿卜杜拉·沙希德。

  • 我需要在UniqueID上加入tableA和tableB,但是我只想加入tableA中具有某些状态限制的最新日期(不是在S中)。我知道我需要使用Max函数,但我不能让它工作。我如何得到下面的结果表? 我想的是:

  • 问题内容: 创建一个将根据年份重置的序列。考虑以9位数开头000000001且最大为999999999的序列。 假设Date为30/12/2017,seq为000012849,所以当日期为01/01/2018时,我希望seq为000000001。 问题答案: 创建一个定期计划作业,该作业在每年的1月1日午夜重新设置顺序。 类似于(假设您有执行重置的过程):

  • 我有一个pandas数据框,我想在一列上执行min、max、mean、median计算,使用列a、B和C对它们进行分组。然后我想将结果合并到初始数据框。当我计算中位数时,我成功地使用了以下命令: 但当我尝试计算最小值和最大值并将其添加到数据帧时,出现以下错误: 列重叠,但没有指定后缀:索引(['Px/SQM'],dtype='对象') 用于最小值或最大值的代码: 我已经尝试使用后缀,它将工作,但我