Delete a duplicate data SQL statement
Method One
Suppose there is a duplicate field of name,address that requires a unique result set for both fields
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)
Method Two
Declare @max integer, @id integer
Declare cur_rows cursor Local for select main field, COUNT (*) from table name Group by main field 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 table name where main field = @id
Fetch cur_rows into @id, @max
End
Close Cur_rows
SET ROWCOUNT 0
Method Three
If the table needs to delete duplicate records (1 of duplicate records are retained), you can delete them in the following ways
SELECT DISTINCT * into #tmp tablename
DROP TABLE TableName
SELECT * INTO TableName from #tmp
drop table #tmp
Instance
Vieworder ID number Punching machine consumption type time price
1 A1 1th, breakfast 2006-08-03 07:09:23.000 2
2 A1 1th, breakfast 2006-08-03 07:10:13.000 2
3 A1 1th, breakfast 2006-08-03 07:10:19.000 2
4 A1 1th, lunch 2006-08-03 12:02:10.000 5
5 A2 1th, lunch 2006-08-03 12:11:10.000 5
6 A2 1th, lunch 2006-08-03 12:12:10.000 5
Code
Delete from Table A
where exists (select * from table where consumption type =a. Consumption type and time >=dateadd (minute,-2,a. Time) and time <a. Time)
View the data to be deleted with the SELECT statement before deleting
SELECT *
from Table A
where exists (SELECT * from table where consumption type =a. Consumption type and Time >= DATEADD (minute,-2,a. Time) and time <a. Time)