我有下表(my_data):
year | X | Y -----+-----+----- 2010 | A | 10 2011 | A | 20 2011 | B | 99 2009 | C | 30 2010 | C | 40
什么是最佳/最小的sql语句,仅检索与最高年份相关的数据并按“X”分组,如下所示:
year | X | Y -----+-----+----- 2011 | A | 20 2011 | B | 99 2010 | C | 40
请注意,此结果表将用于连接.
解决方法@H_403_12@
select year,x,y
from (
select year,y,max(year) over(partition by x) max_year
from my data
)
where year = max_year
select year,x,y from ( select year,y,max(year) over(partition by x) max_year from my data ) where year = max_year