求救各位大侠!
    我想要查询从当前日期开始的前18个月的数据·只取每月的最后一条数据,怎么做?谢谢了

解决方案 »

  1.   

    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
      

  2.   


    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)
      

  3.   


    --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