我有一个像这样的表:
ID Date
1 10/04/2015
1 28/04/2015
1 14/07/2015
1 30/07/2015
1 30/08/2015
2 10/04/2016
2 28/04/2016
2 14/05/2016
2 30/05/2016
但我想实现如下:
ID Date
1 28/04/2015
1 30/07/2015
1 30/08/2015
2 28/04/2016
2 30/05/2016
请你帮助我好吗 。
试试这个:
select * from (
select id,
[Date],
row_number() over (partition by id, datepart(month, [date]) order by [Date] desc) [rn]
from (
select id,
--date convrsion, 103 - British/French - your style
convert(date, [date], 103) [Date]
from @MyTable
) a
) b where rn = 1
我没有真正得到预期结果的逻辑,但下面的查询将起作用。
SELECT *
FROM yourTable
WHERE DAY([Date])>=28;
见How to get Day, Month and Year Part from DateTime in Sql Server