【 전 】 SQL 삭제, 중복 기록 삭제

5095 단어 sql
--           
               ,                ,       ,   
a b c d
1 2 3 4
1 5 3 5
1 2 7 9
 a、b     ,        a、b    ,  ,         1 2 3 4        1 2 7 9
     :
a b c d
1 2 3 4
1 5 3 5
 
a b c d
1 5 3 5
1 2 7 9

        sql     


CREATE TABLE Tb1(id int, [a] varchar(255), [b] varchar(255), [c] varchar(255), [d] varchar(255))
INSERT Tb1(id, [a], [b], [c], [d])
           SELECT 1, '1','2','3','4'
UNION ALL  SELECT 2, '1','5','3','5'
UNION ALL  SELECT 3, '1','2','7','9'
UNION ALL  SELECT 4, '1','4','7','6'

delete Tb1 where [id] not in (select max([id]) from Tb1 group by a,b )
select * from tb1
 
drop table tb1

             
     :
a b c d
1 5 3 5

    :

delete m from tb t
inner join
(
select a ,b
from tb
group by a , b
having count(*)>1
)n
on m.a = n.a and m.b = n.b 
 
delete * from tb as m,
(
select a ,b
from tb
group by a , b
having count(*)>1
)n
where m.a = n.a and m.b = n.b 








------------------------------------------------------------------------------------
       ,         ,    SQL  ,       ?  !
1、           ,           (peopleId)   
select * from people
where peopleId in (select  peopleId  from  people  group  by  peopleId  having  count(peopleId) > 1)

2、           ,           (peopleId)   ,   rowid     
delete from people 
where peopleId  in (select  peopleId  from people  group  by  peopleId   having  count(peopleId) > 1)
and rowid not in (select min(rowid) from  people  group by peopleId  having count(peopleId )>1)

3、           (    ) 
select * from vitae a
where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq  having count(*) > 1)

4、           (    ),   rowid     
delete from vitae a
where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

5、           (    ),   rowid     
select * from vitae a
where (a.peopleId,a.seq) in  (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)

    A        “name”,         “name”       ,
                   ,“name”       ;
Select Name,Count(*) From A Group By Name Having Count(*) > 1

             :
Select Name,sex,Count(*) From A Group By Name,sex Having Count(*) > 1
------------------------------------------------------------------------------------------------
declare @max integer,@id integer
declare cur_rows cursor local for select    ,count(*) from    group by     having count(*) >; 1
open cur_rows
fetch cur_rows into @id,@max
while @@fetch_status=0
begin
select @max = @max -1
set rowcount @max
delete from    where     = @id
fetch cur_rows into @id,@max
end
close cur_rows
set rowcount 0

   
             ,         ,            ,             ,  Name    ,                  。
  1、       ,      ,  
select distinct * from tableName
                。
               (      1 ),         
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp
                    ,           。

  2、                       ,      
           Name,Address,               
select identity(int,1,1) as autoID, * into #Tmp from tableName
select min(autoID) as autoID into #Tmp2 from #Tmp group by Name,autoID
select * from #Tmp where autoID in(select autoID from #tmp2)
      select    Name,Address       (     autoID  ,        select       )
select * from tablename where id in (
select id from tablename 
group by id 
having count(id) > 1)
--18           
DELETE FROM AQSIQ_CLDRC WHERE CODE IN (
      SELECT MAX(CODE),NGRNAME FROM
      (                   
            SELECT * FROM AQSIQ_CLDRC WHERE HBDATE='2013-04-18' AND PARAM2='0' and swrc is null and xwrc is null
             
       ) 
      GROUP BY HBDATE,NGRNAME
)
             SELECT * FROM AQSIQ_CLDRC WHERE NGRNAME IN 
             (      --  18       1   , ngrname
                    SELECT NGRNAME FROM AQSIQ_CLDRC WHERE HBDATE='2013-04-18' GROUP BY NGRNAME HAVING COUNT(*)>1
             )  

좋은 웹페이지 즐겨찾기