表记录:数量   时间
         1 2009-01-01
        1 2009-01-02
        1 2009-01-03
        1 2009-01-04
        2 2009-01-05
        2 2009-01-06
        3 2009-01-07
        1 2009-01-08
        1 2009-01-09
        4 2009-01-10
        4 2009-01-11
        2 2009-01-12
        3 2009-01-13
        3 2009-01-14想得到以下数据:
         1 2009-01-01
        2 2009-01-05
        3 2009-01-07
        1 2009-01-08
        4 2009-01-10
        2 2009-01-12
        3 2009-01-13
表的记录有1000多条,请教如何写SQL实现?

解决方案 »

  1.   


    declare @t table(code int,dt datetime)
    insert into @t select 1,'2009-01-01'
    insert into @t select 1,'2009-01-02'
    insert into @t select 1,'2009-01-03'
    insert into @t select 1,'2009-01-04'
    insert into @t select 2,'2009-01-05'
    insert into @t select 2,'2009-01-06'
    insert into @t select 3,'2009-01-07'
    insert into @t select 1,'2009-01-08'
    insert into @t select 1,'2009-01-09'
    insert into @t select 4,'2009-01-10'
    insert into @t select 4,'2009-01-11'
    insert into @t select 2,'2009-01-12'
    insert into @t select 3,'2009-01-13'
    insert into @t select 3,'2009-01-14'select t.* from  @t t where t.code<>isnull((select top 1 code from @t where dt<t.dt order by dt desc),0)/*
    code        dt                                                     
    ----------- ------------------------------------------------------ 
    1           2009-01-01 00:00:00.000
    2           2009-01-05 00:00:00.000
    3           2009-01-07 00:00:00.000
    1           2009-01-08 00:00:00.000
    4           2009-01-10 00:00:00.000
    2           2009-01-12 00:00:00.000
    3           2009-01-13 00:00:00.000
    */
      

  2.   

    libin_ftsafe 真强
    我都没搞懂楼主要什么
    你的结果都拿出来了