有M,N 两个列,数据如下:
M N
A 87
A 70
A 32
B 99
B 62
B 23
C 444
C 351
C 97
数据规则:可以把M列当成学生编号,N列当成学生成绩,现在要取的是每个学生最好的前两门成绩,和编号一起显示出来,比如A学生的结果是
M N
A 87
A 70
哪位知道这样的SQL怎么写吗?
select m,max(n) as cj from datetable group by m
union all
select m,max(n) as cj from datatable a , (select m,max(n) as cj from datetable group by m ) b where a.m=b.m and a.n<>b.cj group by m
分组排序的问题啊:
select * from( select studentID,score, row_number() over (partition by score order by studentID desc) as rowno from table1) A where a.rowno<=2
这种方式感觉更容易理解