guid   datetime   gasstation
1      2009-10-1       2
2      2009-10-1       3 
3      2009-10-2       2
4      2009-10-2       2 
5      2009-10-3       2
6      2009-10-3       4 要求删除同一天内,gasstation号码不同的数据,保留guid号较小的那条数据,删除后结果:guid   datetime   gasstation
1      2009-10-1       2
3      2009-10-2       2
4      2009-10-2       2 
5      2009-10-3       2

解决方案 »

  1.   

    delete from TB A where not exists (select 1 from TB where datetime =A.datetime  AND A.gasstation>gasstation )
      

  2.   

    delete  a from table1 as a where exists(select 1 from table1 where datediff(dd,a.[datetime],[datetime])=0 and guid<a.guid)
      

  3.   

    delete  a from table1 as a where exists(select 1 from table1 where datediff(dd,a.[datetime],[datetime])=0 and guid<a.guid and gasstation=a.gasstation)
      

  4.   

    delete tb from tb t where guid not in (select min(guid) from tb where datetime = t.datetime)
      

  5.   

    delete tb from tb t where guid not in (select min(guid) from tb where gasstation = t.gasstation and datetime = t.datetime)delete tb from tb t where not exists (select 1 from tb where gasstation = t.gasstation and datetime = t.datetime and guid < t.guid)
      

  6.   

    --不过考虑到你要求的同天,建议如下:delete tb from tb t where guid not in (select min(guid) from tb where gasstation = t.gasstation and datediff(dd,datetime , t.datetime) = 0)delete tb from tb t where not exists (select 1 from tb where gasstation = t.gasstation and datediff(dd,datetime , t.datetime) = 0 and guid < t.guid)
      

  7.   

    use Tempdb
    go
    --> --> 
     
    if not object_ID(N'Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([guid] int,[datetime] Datetime,[gasstation] int)
    Insert #T
    select 1,'2009-10-1',2 union all
    select 2,'2009-10-1',3 union all
    select 3,'2009-10-2',2 union all
    select 4,'2009-10-2',2 union all
    select 5,'2009-10-3',2 union all
    select 6,'2009-10-3',4
    Go
    DELETE a from #T AS a
    WHERE EXISTS(SELECT 1 FROM #T WHERE DATEDIFF(dd,[datetime],a.[datetime])=0 HAVING COUNT(DISTINCT [gasstation])>1 )
    AND EXISTS(SELECT 1 FROM #T WHERE DATEDIFF(dd,[datetime],a.[datetime])=0 AND [guid]<a.[guid] )goSELECT * FROM #T
    /*
    guid datetime gasstation
    1 2009-10-01 00:00:00.000 2
    3 2009-10-02 00:00:00.000 2
    4 2009-10-02 00:00:00.000 2
    5 2009-10-03 00:00:00.000 2
    */
      

  8.   

    或use Tempdb
    go
    --> --> 
     
    if not object_ID(N'Tempdb..#T') is null
    drop table #T
    Go
    Create table #T([guid] int,[datetime] Datetime,[gasstation] int)
    Insert #T
    select 1,'2009-10-1',2 union all
    select 2,'2009-10-1',3 union all
    select 3,'2009-10-2',2 union all
    select 4,'2009-10-2',2 union all
    select 5,'2009-10-3',2 union all
    select 6,'2009-10-3',4
    GoDELETE b
    FROM (SELECT [datetime] FROM #T GROUP BY [datetime] HAVING COUNT(DISTINCT [gasstation])>1)a
    INNER JOIN #T AS b ON a.[datetime]=b.[datetime]
    WHERE  EXISTS(SELECT 1 FROM #T WHERE [datetime]=b.[datetime] AND [guid]<b.[guid] )goSELECT * FROM #T
    /*
    guid datetime gasstation
    1 2009-10-01 00:00:00.000 2
    3 2009-10-02 00:00:00.000 2
    4 2009-10-02 00:00:00.000 2
    5 2009-10-03 00:00:00.000 2
    */
      

  9.   


    create table #t([guid] int,[datetime] datetime,gasstation int);
    insert into #t select 1,'2009-10-1',2 union all 
    select 2,'2009-10-1',3 union all
    select 3,'2009-10-2',2 union all
    select 4,'2009-10-2',2 union all
    select 5,'2009-10-3',2 union all
    select 6,'2009-10-3',4 union all
    select 7,'2009-10-3',3 union all
    select 8,'2009-10-3',3;--select * from #t;
    delete a from #t a where exists(select 1 from #t b where b.datetime=a.[datetime] and b.gasstation<a.gasstation);
    select * from #t;drop table #t;/*
    guid        datetime                gasstation
    ----------- ----------------------- -----------
    1           2009-10-01 00:00:00.000 2
    3           2009-10-02 00:00:00.000 2
    4           2009-10-02 00:00:00.000 2
    5           2009-10-03 00:00:00.000 2
    */
      

  10.   

    删除重复记录有大小关系时,保留大或小其中一个记录
    --> --> (Roy)生成測試數據if not object_id('Tempdb..#T') is null
        drop table #T
    Go
    Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
    Insert #T
    select 1,N'A',N'A1' union all
    select 2,N'A',N'A2' union all
    select 3,N'A',N'A3' union all
    select 4,N'B',N'B1' union all
    select 5,N'B',N'B2'
    Go--I、Name相同ID最小的记录(推荐用1,2,3),保留最小一条
    方法1:
    delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID<a.ID)方法2:
    delete a  from #T a left join (select min(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null方法3:
    delete a from #T a where ID not in (select min(ID) from #T where Name=a.Name)方法4(注:ID为唯一时可用):
    delete a from #T a where ID not in(select min(ID)from #T group by Name)方法5:
    delete a from #T a where (select count(1) from #T where Name=a.Name and ID<a.ID)>0方法6:
    delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID)方法7:
    delete a from #T a where ID>any(select ID from #T where Name=a.Name)select * from #T生成结果:
    /*
    ID          Name Memo
    ----------- ---- ----
    1           A    A1
    4           B    B1(2 行受影响)
    */
    --II、Name相同ID保留最大的一条记录:方法1:
    delete a from #T a where  exists(select 1 from #T where Name=a.Name and ID>a.ID)方法2:
    delete a  from #T a left join (select max(ID)ID,Name from #T group by Name) b on a.Name=b.Name and a.ID=b.ID where b.Id is null方法3:
    delete a from #T a where ID not in (select max(ID) from #T where Name=a.Name)方法4(注:ID为唯一时可用):
    delete a from #T a where ID not in(select max(ID)from #T group by Name)方法5:
    delete a from #T a where (select count(1) from #T where Name=a.Name and ID>a.ID)>0方法6:
    delete a from #T a where ID<>(select top 1 ID from #T where Name=a.name order by ID desc)方法7:
    delete a from #T a where ID<any(select ID from #T where Name=a.Name)
    select * from #T
    /*
    ID          Name Memo
    ----------- ---- ----
    3           A    A3
    5           B    B2(2 行受影响)
    */
      

  11.   

    delete a from t1 as a
    where not exists (select 1 from t1 where a.guid>guid and a.[datetime]=[datatime])