如何从一组复合行中找到最大值?

时间:2012-10-16 05:43:42

标签: mysql sql max composite

我有一张表如:

Year | Month
------------
2011    10   
2011    11   
2012    5   
2012    6 

查询应返回最新的"月"最新的"年"。

目前我正在做类似

的事情
  

从表中选择MAX(" Month")" Year" in(从表中选择MAX(" Year")

但我对查询不满意。有人可以提出更紧凑,更清洁的方式吗?

2 个答案:

答案 0 :(得分:2)

试试这个

select top 1
    "Month"
from table
order by "Year" desc, "Month" desc

好吧,对于MySQL,我认为它应该是

select
    "Month"
from table
order by "Year" desc, "Month" desc
limit 1

答案 1 :(得分:1)

试试这个:

select t1.months from
(select top 1 t.months as months,max(t.years) as years from
(select years,max(months) as months from cal group by years) t
group by t.months) t1