使用max ids选择id

时间:2018-01-31 11:30:22

标签: mysql sql

我有一张桌子,看起来像这样:

enter image description here

如何获得具有最大年份和sem的所有ID?

SELECT  m.ID,
    MAX(m.Year) as MaxYearLevel,
    m.Sem
    FROM
(SELECT t1.ID AS StudentID,
        t1.Year,
        t1.Sem
 FROM table1 t1) AS m
 GROUP BY m.ID; 

1 个答案:

答案 0 :(得分:0)

只需按

执行订单
Select * from [Your table] order by year DESC, sem DESC

并指定您要拍摄的行数

ORACLE SQL:

Select * from [Your table] order by year DESC, sem DESC fetch first x rows only

SQL:

select Top(x) * from [Your table] order by year DESC, sem DESC