id lat long speed date address
1 22.92138131 72.44103313 3.96 km/h 2011-09-26 National, Gujarat, India
2 22.92138145 72.44103413 13.96 km/h 2011-09-26 National, Gujarat, India
3 22.92138134 72.44103423 15.96 km/h 2011-09-26 National, Gujarat, India
4 22.92138454 72.44103233 13.96 km/h 2011-09-26 10t ring Rd, Nehru Nagar
5 22.92138354 72.44102533 13.96 km/h 2011-09-26 Anandnagar Rd, Ahmedabad
6 22.92138484 72.44103293 19.96 km/h 2011-09-26 Anandnagar Rd, Ahmedabad
I want to write a query such that my result looks like this:
id lat long speed date address
1 22.92138131 72.44103313 3.96 km/h 2011-09-26 National, Gujarat, India
4 22.92138454 72.44103233 13.96 km/h 2011-09-26 10t ring Rd, Nehru Nagar
5 22.92138354 72.44102533 13.96 km/h 2011-09-26 Anandnagar Rd, Ahmedabad
I want to remove duplicate rows according to the address.
SQL Delete Duplicate Rows using Group By and Having Clause According to Delete Duplicate Rows in SQL, for finding duplicate rows, you need to use the SQL GROUP BY clause. The COUNT function can be used to verify the occurrence of a row using the Group by clause, which groups data according to the given columns.
So to delete the duplicate record with SQL Server we can use the SET ROWCOUNT command to limit the number of rows affected by a query. By setting it to 1 we can just delete one of these rows in the table. Note: the select commands are just used to show the data prior and after the delete occurs.
Remove Duplicate Rows in Excel Select the entire data. Go to Data –> Data Tools –> Remove Duplicates. In the Remove Duplicates dialog box: If your data has headers, make sure the 'My data has headers' option is checked.
To check what you are going to delete:
SELECT distinct t1.*
FROM yourtable as t1
join yourtable as t2
WHERE t1.address = t2.address
and t1.id < t2.id
If you are happy with that:
DELETE t1
FROM yourtable as t1
join yourtable as t2
WHERE t1.address = t2.address
and t1.id < t2.id
This way you keep the record with the max value on id column
If you don't care which of the rows you keep
ALTER IGNORE TABLE table ADD UNIQUE KEY 'address' (`address`);
The 'IGNORE' is important, that means to silently ignore duplicate data. (ie ignores it when inserting into the 'new version' of the table.)
May want to remove the index afterwoods
ALTER TABLE table DROP KEY 'address';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With