SQL SERVERÊý¾Ý¿âÌ«´ó£¬Ó°ÏìÐÔÄÜ£¬Æ©ÈçÓ¦ÓòéѯËٶȵȵȣ¬ÓÐʱºòÐèÒªÇå³ýÈÕÖ¾¼Ç¼¡¢ÎÞÓÃÐÅÏ¢£¬É¾³ýÖظ´Êý¾Ý£¬ÒÔ¼õÉÙÊý¾Ý¿â¶Ôϵͳ×ÊÔ´µÄ¿ªÖ§¡£
ÀýÈ磺 id           name         value 1               a                 pp 2               a                 pp 3               b                 iii 4               b                 pp 5               b                 pp 6               c                 pp 7               c                 pp 8               c                 iii idÊÇÖ÷¼ü ÒªÇóµÃµ½ÕâÑùµÄ½á¹û id           name         value 1               a                 pp 3               b                 iii 4               b                 pp 6               c                 pp 8               c                 iii
·½·¨1 delete   YourTable   where   [id]   not   in   ( select   max([id])   from   YourTable   group   by   (name   +   value))
·½·¨2 delete   a from   ±í   a   left   join( select   id=min(id)   from   ±í   group   by   name,value )b   on   a.id=b.id where   b.id   is   null
²éѯ¼°É¾³ýÖظ´¼Ç¼µÄSQLÓï¾ä ²éѯ¼°É¾³ýÖظ´¼Ç¼µÄ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
·½·¨¶þ £¢Öظ´¼Ç¼£¢ÓÐÁ½¸öÒâÒåÉϵÄÖظ´¼Ç¼£¬Ò»ÊÇÍêÈ«Öظ´µÄ¼Ç¼£¬Ò²¼´ËùÓÐ×ֶξùÖظ´µÄ¼Ç¼£¬¶þÊDz¿·Ö¹Ø¼ü×Ö¶ÎÖظ´µÄ¼Ç¼£¬±ÈÈçName×Ö¶ÎÖظ´£¬¶øÆäËû×ֶβ»Ò»¶¨Öظ´»ò¶¼Öظ´¿ÉÒÔºöÂÔ¡£ ¡¡¡¡1¡¢¶ÔÓÚµÚÒ»ÖÖÖظ´£¬±È½ÏÈÝÒ×½â¾ö£¬Ê¹Óà select distinct * from tableName ¡¡¡¡¾Í¿ÉÒԵõ½ÎÞÖظ´¼Ç¼µÄ½á¹û¼¯¡£ ¡¡¡¡Èç¹û¸Ã±íÐèҪɾ³ýÖظ´µÄ¼Ç¼£¨Öظ´¼Ç¼±£Áô1Ìõ£©£¬¿ÉÒÔ°´ÒÔÏ·½·¨É¾³ý select distinct * into #Tmp from tableName drop table tableName select * into tableName from #Tmp drop table #Tmp ¡¡¡¡·¢ÉúÕâÖÖÖظ´µÄÔÒòÊDZíÉè¼Æ²»ÖܲúÉúµÄ£¬Ôö¼ÓΨһË÷ÒýÁм´¿É½â¾ö¡£ ¡¡¡¡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 ) |