Deleting all but the most recent entry from single SQL table

rocky

I have a single SQL table that contains multiple entries for each customerID (some customerID's only have one entry which I want to keep). I need to remove all but the most recent entry per customerID, using the invoiceDate field as my marker.

So I need to go from this:

+------------+-------------+-----------+
| customerID | invoiceDate | invoiceID |
+------------+-------------+-----------+
|          1 |  1393995600 |       xx  |
|          1 |  1373688000 |       xx  |
|          1 |  1365220800 |       xx  |
|          2 |  1265220800 |       xx  |
|          2 |  1173688000 |       xx  |
|          3 |  1325330800 |       xx  |
+------------+-------------+-----------+

To this:

+------------+-------------+-----------+
| customerID | invoiceDate | invoiceID |
+------------+-------------+-----------+
|          1 |  1393995600 |       xx  |
|          2 |  1265220800 |       xx  |
|          3 |  1325330800 |       xx  |
+------------+-------------+-----------+

Any guidance would be greatly appreciated!

pyb
  1. Write a query to select all the rows you want to delete:
SELECT * FROM t
WHERE invoiceDate NOT IN (
    SELECT MAX(invoiceDate)
    -- "FROM t AS t2" isn't supported by MySQL, see http://stackoverflow.com/a/14302701/227576
    FROM (SELECT * FROM t) AS t2
    WHERE t2.customerId = t.customerId
    GROUP BY t2.customerId
)

This may take a long time on a big database.

  1. If you're satisfied, change the query to a DELETE statement:
DELETE FROM t
WHERE invoiceDate NOT IN (
    SELECT MAX(invoiceDate)
    -- "FROM t AS t2" isn't supported by MySQL, see http://stackoverflow.com/a/14302701/227576
    FROM (SELECT * FROM t) AS t2
    WHERE t2.customerId = t.customerId
    GROUP BY t2.customerId
)

See http://sqlfiddle.com/#!9/6e031/1

If you have multiple rows whose date is the most recent for the same customer, you would have to look for duplicates and decide which one you want to keep yourself. For instance, look at customerId 2 on the SQL fiddle link above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Select Most Recent Entry in SQL

From Dev

SQL: Filtering the most recent date entry from a Select statement

From Dev

Deleting the most recent submission from database

From Dev

How do I get the most recent entry from a table using LINQ?

From Dev

How do I get the most recent entry from a table using LINQ?

From Dev

select the most recent entry

From Dev

SQL UPDATE to update the most recent date in table

From Dev

SQLite: How to SELECT "most recent record for each user" from single table with composite key?

From Dev

SQL - Compare row changes with most recent and previous entry

From Dev

SQL: Querying several tables, only most recent result from one, and all results from another

From Dev

SQL: Querying several tables, only most recent result from one, and all results from another

From Dev

SQL Query where I get most recent rows from timestamp from another table

From Dev

SQL Get most recent date from table to be included in a VIEW with inner join

From Dev

Deleting entry with the highest value from a table

From Dev

Deleting an active record entry from a joined table without deleting the orginal

From Dev

How to delete all records in an access table except most recent

From Dev

Get the change in price for all most recent prices in T-SQL

From Dev

SQL Inner Join Most Recent Row Of Xref Table

From Dev

SQL Selecting Most Recent Date (W/ Three Table Join)

From Dev

PHP select and mark recent entry from all MYSQL results

From Dev

Delete all rows from account, except most recent eleven

From Dev

Most recent distinct record from a joined MySQL table

From Dev

How to Select the most recent items by dates from another table

From Dev

msaccess join most recent matching record from one table to another

From Dev

Update with JOIN from another table and get the most recent

From Dev

How do I grab the most recent price change from table

From Dev

Using group by to get most recent record from table

From Dev

msaccess join most recent matching record from one table to another

From Dev

MYSQL:: Selecting rows from table without the most recent ones

Related Related

  1. 1

    Select Most Recent Entry in SQL

  2. 2

    SQL: Filtering the most recent date entry from a Select statement

  3. 3

    Deleting the most recent submission from database

  4. 4

    How do I get the most recent entry from a table using LINQ?

  5. 5

    How do I get the most recent entry from a table using LINQ?

  6. 6

    select the most recent entry

  7. 7

    SQL UPDATE to update the most recent date in table

  8. 8

    SQLite: How to SELECT "most recent record for each user" from single table with composite key?

  9. 9

    SQL - Compare row changes with most recent and previous entry

  10. 10

    SQL: Querying several tables, only most recent result from one, and all results from another

  11. 11

    SQL: Querying several tables, only most recent result from one, and all results from another

  12. 12

    SQL Query where I get most recent rows from timestamp from another table

  13. 13

    SQL Get most recent date from table to be included in a VIEW with inner join

  14. 14

    Deleting entry with the highest value from a table

  15. 15

    Deleting an active record entry from a joined table without deleting the orginal

  16. 16

    How to delete all records in an access table except most recent

  17. 17

    Get the change in price for all most recent prices in T-SQL

  18. 18

    SQL Inner Join Most Recent Row Of Xref Table

  19. 19

    SQL Selecting Most Recent Date (W/ Three Table Join)

  20. 20

    PHP select and mark recent entry from all MYSQL results

  21. 21

    Delete all rows from account, except most recent eleven

  22. 22

    Most recent distinct record from a joined MySQL table

  23. 23

    How to Select the most recent items by dates from another table

  24. 24

    msaccess join most recent matching record from one table to another

  25. 25

    Update with JOIN from another table and get the most recent

  26. 26

    How do I grab the most recent price change from table

  27. 27

    Using group by to get most recent record from table

  28. 28

    msaccess join most recent matching record from one table to another

  29. 29

    MYSQL:: Selecting rows from table without the most recent ones

HotTag

Archive