以下是我的数据
date value
30/03/2014 625949
23/03/2014 624549
16/03/2014 623149
09/03/2014 621549
02/03/2014 619749
23/02/2014 617749
16/02/2014 616149
09/02/2014 614549
02/02/2014 612949
19/01/2014 609749
12/01/2014 608149
06/01/2014 606749
如果我想只获得一个月中的最后一个值,例如下面的输出,我怎么能得到它?
date value
30/03/2014 625949
23/02/2014 617749
19/01/2014 609749
答案 0 :(得分:5)
;with Cte as(Select tDate,value, C=ROW_NUMBER()
over(PARTITION by convert(varchar(6), tdate, 112) order by tdate desc)
From #Temp )
Select *
from cte
where C=1
小提琴Sample
答案 1 :(得分:1)
SELECT date,value FROM table1 WHERE date in (
SELECT MAX(date) FROM table1 GROUP BY MONTH(date)
)