select * from (select px=row_number()over(partition by month(time) order by getdate()),* from tb)t where px=1 and datediff(mm,time,getdate())<=18
select t1.* from table1 t1 where FDate>=dateadd(month,-18,getdate()) and not exists(select 1 from table1 t2 where convert(varchar(6),t1.FDate,112)=convert(varchar(6),t2.FDate,112) and t1.FDate<t2.FDate)
--SQL2005 select * from (select px=row_number()over(partition by convert(varchar(6),time,112) order by time desc),* from tb)t where px=1 and datediff(mm,time,getdate())<=18
*
from
(select px=row_number()over(partition by month(time) order by getdate()),* from tb)t
where
px=1
and
datediff(mm,time,getdate())<=18
select t1.*
from table1 t1
where FDate>=dateadd(month,-18,getdate())
and not exists(select 1 from table1 t2
where convert(varchar(6),t1.FDate,112)=convert(varchar(6),t2.FDate,112)
and t1.FDate<t2.FDate)
--SQL2005
select
*
from
(select px=row_number()over(partition by convert(varchar(6),time,112) order by time desc),* from tb)t
where
px=1
and datediff(mm,time,getdate())<=18