现在有一张表,有年、月、日、时、分,数据值这几个字段,把年月日时分设置为主键的话就说有重复数据,用什么SQL语句能过滤出来重复的值,把其删除

解决方案 »

  1.   

    假设还有个字段ID,能根据年、月、日、时、分区分大小。delete tb from tb t where id not in (select min(id) from tb where 年 = t.年 and 月=t.月 and 日 = t.日 and 时 = t.时 and 分=t.分)
      

  2.   


    select * from 表名1 a where not exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
    and 表名.时 = a.时 and 表名.分 = a.分)我是这么写的,但是没过滤出来有重复数据呢,您写的假设没ID这个字段呢
      

  3.   

    select * 
    from 表名1 a 
    where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
    and 表名.时 = a.时 and 表名.分 = a.分)
    这样就过滤后粗来了
      

  4.   

    select * from 表名1 a where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
    and 表名.时 = a.时 and 表名.分 = a.分)
      

  5.   

    ;with cte as
    (
        select rn = row_number()over(partition by 年,月,日,时,分 order by getdate()), * from 有一张表
    )
    delete from cte where rn > 1
      

  6.   

    use test
    go
    if object_id('test.dbo.tb') is not null drop table tb
    -- 创建数据表
    create table tb
    (
    年 int,
    月 int,
    日 int,
    时 int,
    分 int
    )
    go
    --插入测试数据
    insert into tb select 2010,3,5,10,35
    union all select 2010,3,5,10,35
    union all select 2010,3,5,10,35
    union all select 2010,7,15,9,24
    union all select 2010,7,15,9,24
    union all select 2010,9,20,6,43
    go
    --代码实现select * from tb/*测试结果年 月 日 时 分
    ---------------------------------------------
    2010 3 5 10 35
    2010 3 5 10 35
    2010 3 5 10 35
    2010 7 15 9 24
    2010 7 15 9 24
    2010 9 20 6 43(6 行受影响)
    */;with t as(select idd=row_number()over(partition by 年,月,日,时,分 order by getdate()),* from tb)
    delete t from t where idd!=1select * from tb/*测试结果年 月 日 时 分
    ---------------------------------------------
    2010 3 5 10 35
    2010 7 15 9 24
    2010 9 20 6 43(3 行受影响)
    */
      

  7.   

    select * from 表名1 a where exists (select 1 from 表名 where 表名.年=a.年 and 表名.月=a.月 and 表名.日= a.日
    and 表名.时 = a.时 and 表名.分 = a.分)
      

  8.   


    delete a from tb a where exists (select 1 from (select row_number() over(order by getdate()) as id,* from tb) x where a.id>x.id and a.年=x.年 and a.月=x.月 and a.日=x.日 and a.时=x.时 and a.分=x.分)
      

  9.   


    delete a from tb as a where ID not exists(select max(ID) from tb)