表kk
字段一   字段二  字段三  字段四
sk       yes     aaaa    2005-02-02
sk       yes     aaaa    2005-02-03
sc       noo     bbbb    2005-02-09
sc       noo     bbbb    2005-02-05我要得到结果
sk       yes     aaaa    2005-02-02
sc       noo     bbbb    2005-02-05也就是前面三个字段相同的作为重复数据,
只保留一条时间最早的数据,SQL怎么写?
谢谢!

解决方案 »

  1.   

    declare @t table(字段一 varchar(10),字段二  varchar(10),字段三 varchar(10),字段四 varchar(10))
    insert into @t select 'sk'   ,    'yes'   ,  'aaaa'   , '2005-02-02'
    union all select 'sk'     ,  'yes'   ,  'aaaa'   , '2005-02-03'
    union all select 'sc'    ,   'noo'  ,   'bbbb'  ,  '2005-02-09'
    union all select 'sc'    ,   'noo'  ,   'bbbb'  ,  '2005-02-05'delete @t from @t a where exists(select 1 from @t where 字段一=a.字段一 and 字段二=a.字段二 and 字段三=a.字段三 and 字段四<a.字段四)select * from @t
      

  2.   

    select 字段一,   字段二,  字段三,  min(字段四) as 字段四
    from  表kk
    group by 字段一 ,  字段二,  字段三
      

  3.   

    delete 表kk from 表kk a where exists(select 1 from @t where 字段一=a.字段一 and 字段二=a.字段二 and 字段三=a.字段三 and 字段四<a.字段四)
      

  4.   

    select * from kk k1
    where not exists (select 1 from kk k2 where k2.字段1=k1.字段1 and k2.字段2=k1.字段2 and k2.字段3 = k1.字段3 and k1.字段4>k2.字段4)
      

  5.   

    select distinct * into #tb from tb
    truncate tb
    insert into tb select * from #tb
      

  6.   

    create table t3(f1 nvarchar(10),f2 nvarchar(10),f3 nvarchar(10),f4 datetime)
    insert into t3 select 'sk','yes','aaa','2005-02-02'
         union all select 'sk','yes','aaa','2005-02-03'
         union all select 'sc','noo','bbb','2005-02-09'
         union all select 'sc','noo','bbb','2005-02-05'
    --select * from t3
    select f1,f2,f3,min(f4) from t3
    group by f1,f2,f3drop table t3
      

  7.   

    delete a
    from kk a 
    where exists(select * from kk where 字段1=a.字段1 and 字段2=a.字段2
    and 字段3=a.字段3 and 字段4>a.字段4)

    delete a
    from kk a,kk b
    where  b.字段1=a.字段1 and b.字段2=a.字段2
    and b.字段3=a.字段3 and b.字段4>a.字段4)
      

  8.   

    b.字段4>a.字段4是不对的呀,我要取多个时间当中最小的一个呀
      

  9.   

    --创建表
    create table t3(f1 nvarchar(10),f2 nvarchar(10),f3 nvarchar(10),f4 datetime)
    insert into t3 select 'sk','yes','aaa','2005-02-02'
         union all select 'sk','yes','aaa','2005-02-03'
         union all select 'sc','noo','bbb','2005-02-09'
         union all select 'sc','noo','bbb','2005-02-05'
    --执行查询
    delete t3 
    from t3 a
    where exists(select * from t3 where f1=a.f1 and f2=a.f2 and f3=a.f3 and f4<a.f4)--select * from t3
    --删除表
    DROP table t3
      

  10.   

    sk       yes     aaaa    2005-02-02
    sk       yes     aaaa    2005-02-03
    sc       noo     bbbb    2005-02-09
    sc       noo     bbbb    2005-02-05
    sc       noo     bbbb    2005-02-06
    sc       noo     bbbb    2005-02-07
    sc       noo     bbbb    2005-02-08
    要得到
    sk       yes     aaaa    2005-02-02
    sc       noo     bbbb    2005-02-05
    如果是这样的数据话,你使用的f4<a.f4条件是不行的
      

  11.   

    可以啊,你试一下就知道了~--创建表
    create table t3(f1 nvarchar(10),f2 nvarchar(10),f3 nvarchar(10),f4 datetime)
    insert into t3 select 'sk','yes','aaaa','2005-02-02'
         union all select 'sk','yes','aaaa','2005-02-03'
         union all select 'sc','noo','bbbb','2005-02-09'
         union all select 'sc','noo','bbbb','2005-02-05'
         union all select 'sc','noo','bbbb','2005-02-06'
         union all select 'sc','noo','bbbb','2005-02-07'
         union all select 'sc','noo','bbbb','2005-02-08'
    --执行查询
    delete t3 
    from t3 a
    where exists(select * from t3 where f1=a.f1 and f2=a.f2 and f3=a.f3 and f4<a.f4)--select * from t3
    --删除表
    DROP table t3
      

  12.   

    懊!!我的失误,感谢大家,特别感谢itblog(I like I do)