sql 分组后查询最大所有列信息
Posted 刘奇云
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 分组后查询最大所有列信息相关的知识,希望对你有一定的参考价值。
CREATE TABLE students (course varchar(10), stu_name varchar(10), city varchar(10), score int ) insert into students values(\'数学\',\'Jack\',\'Tianjin\',80); insert into students values(\'数学\',\'Jordan\',\'Tianjin\',80); insert into students values(\'数学\',\'James\',\'Beijing\',55); insert into students values(\'英语\',\'Jack\',\'Tianjin\',90); insert into students values(\'英语\',\'Jordan\',\'Tianjin\',60); insert into students values(\'英语\',\'James\',\'Beijing\',100); insert into students values(\'语文\',\'Jack\',\'Tianjin\',60); insert into students values(\'语文\',\'Tom\',\'Tianjin\',77); insert into students values(\'语文\',\'Jordan\',\'Tianjin\',68); DELETE from students SELECT * from students --最大是多条的时候取一条 select * from ( select course,stu_name,city,score,ROW_NUMBER() over(partition by course order by score desc) as rn from students ) a where a.rn <=1; --最大是多条的时候都取 select --a.course,a.score,b.stu_name,b.city,b.course course1,b.score score1 b.* from ( SELECT course,max(score) score from students GROUP BY course ) a LEFT JOIN students b on a.course=b.course and a.score=b.score;
以上是关于sql 分组后查询最大所有列信息的主要内容,如果未能解决你的问题,请参考以下文章
SQL语句汇总(三)——聚合函数分组子查询及组合查询 - Darly
Sqlserver 中 如何筛选出 分组后成绩最大列的这一列