Num CI_A CellName_A Bearing_A CI_B CellName_B Bearing_B
2 10141 海口天竺花园1 40 10142 海口天竺花园2 140
3 10141 海口天竺花园1 40 10143 海口天竺花园3 320
4 10142 海口天竺花园2 140 10141 海口天竺花园1 40
5 10142 海口天竺花园2 140 10143 海口天竺花园3 320
6 10143 海口天竺花园3 320 10141 海口天竺花园1 40
7 10143 海口天竺花园3 320 10142 海口天竺花园2 140
Num CI_A CellName_A Bearing_A CI_B CellName_B Bearing_B
2 10141 海口天竺花园1 40 10142 海口天竺花园2 140
5 10142 海口天竺花园2 140 10143 海口天竺花园3 320
6 10143 海口天竺花园3 320 10141 海口天竺花园1 40对调相同的CI_A与CI_B,只保留一条记录,SQL语句,如何编写,谢谢!

解决方案 »

  1.   

    DELETE T FROM TB T WHERE EXISTS(SELECT 1 FROM TB WHERE CI_A=T.CI_A AND Num>T.NUM)
      

  2.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-03-10 12:05:39
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    -- Aug  6 2000 00:57:48 
    -- Copyright (c) 1988-2000 Microsoft Corporation
    -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#TB
    if object_id('tempdb.dbo.#TB') is not null drop table #TB
    go 
    create table #TB([Num] int,[CI_A] int,[CellName_A] varchar(13),[Bearing_A] int,[CI_B] int,[CellName_B] varchar(13),[Bearing_B] int)
    insert #TB
    select 2,10141,'海口天竺花园1',40,10142,'海口天竺花园2',140 union all
    select 3,10141,'海口天竺花园1',40,10143,'海口天竺花园3',320 union all
    select 4,10142,'海口天竺花园2',140,10141,'海口天竺花园1',40 union all
    select 5,10142,'海口天竺花园2',140,10143,'海口天竺花园3',320 union all
    select 6,10143,'海口天竺花园3',320,10141,'海口天竺花园1',40 union all
    select 7,10143,'海口天竺花园3',320,10142,'海口天竺花园2',140
    --------------开始查询--------------------------DELETE T FROM #TB T 
    WHERE EXISTS(SELECT 1 FROM #TB WHERE CI_A=T.CI_b and CI_B=T.CI_A AND Num<T.NUM)SELECT * FROM #TB----------------结果----------------------------
    /* (所影响的行数为 6 行)
    (所影响的行数为 3 行)Num         CI_A        CellName_A    Bearing_A   CI_B        CellName_B    Bearing_B   
    ----------- ----------- ------------- ----------- ----------- ------------- ----------- 
    2           10141       海口天竺花园1       40          10142       海口天竺花园2       140
    3           10141       海口天竺花园1       40          10143       海口天竺花园3       320
    5           10142       海口天竺花园2       140         10143       海口天竺花园3       320(所影响的行数为 3 行)
    */
      

  3.   

    2、删除重复记录有大小关系时,保留大或小其中一个记录
    --> --> (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 行受影响)
    */