我建了一张表,里边包括 ID,时间和 TAG,表里很多TAG和ID是重复的,现在我想去除TAG的重复,但要获取最新时间的ID,语句该怎么写?

解决方案 »

  1.   

    create table #t(tag int, _date datetime)
    insert #t select 1, '2006-06-07'
    union all select 1,'2007-02-03'
    union all select 1,'2007-02-04'
    union all select 2,'2007-02-03'delete from #t 
    where convert(varchar,tag) + '_' + convert(varchar,_date ,120)
    not in (select convert(varchar,tag) + '_' + convert(varchar,max(_date),120) from #t group by tag)select * from #t/*
    tag         _date                                                  
    ----------- ------------------------------------------------------ 
    1           2007-02-04 00:00:00.000
    2           2007-02-03 00:00:00.000
    */drop table #t
      

  2.   

    http://community.csdn.net/Expert/topic/5073/5073353.xml?temp=.7302973
      

  3.   

    select identity(int,1,1) as autoID, * into #Tmp from tableName
    select min(autoID) as autoID into #Tmp2 from #Tmp group by ID,autoID
    select * from #Tmp where autoID in(select autoID from #tmp2) 
    最后一个select即得到了ID,Tag不重复的结果集(但多了一个autoID字段,实际写时可以写在select子句中省去此列)