我在数据库中用有表
stugradegradeid  xuehao   grade   updatetime  comment
1        2003611   89     2006-9-13
2        2003611   90     2006-9-9
3        2003615   92     2006-8-25
4        2003615   85     2006-8-30现在我想要每个学生(xuehao)的最后修改的记录(也就是删除以前的记录)
这个该怎么做到啊谢谢

解决方案 »

  1.   

    delete a 
    from stugrade a
    where not exists(
        select * from stugrade where xuehao=a.xuehao and updatetime>a.updatetime)
      

  2.   


    --如果某个学号只有一条记录,要保留
    delete a 
    from stugrade a
    where exists(
        select * from stugrade where xuehao=a.xuehao and updatetime>a.updatetime)
      

  3.   

    declare @t table (gradeid int,xuehao int,grade int, updatetime datetime)
    insert into @t
    select 1,2003611,89,'2006-9-13'
    union all
    select 2,2003611,90,'2006-9-9'
    union all
    select 3,2003615,92,'2006-8-25'
    union all
    select 4,2003615,85,'2006-8-30'
    union all
    select 5,2003616,85,'2007-01-01'delete a from @t a
    where updatetime<>(select max(updatetime) from @t where xuehao=a.xuehao)
    select * from @t
    结果:
    1 2003611 89 2006-09-13 00:00:00.000
    4 2003615 85 2006-08-30 00:00:00.000
    5 2003616 85 2007-01-01 00:00:00.000
      

  4.   

    select id1=identity(int,1,1),* into #t from a
    go
    select id,name from #t where id1 in(select max(id1) from #t group by id)