I have a table which contains sales order data (order number, product number, sales price, etc.).
However, the table is littered with corrections and various other invalid data. One of the main issues is that corrections were entered by adding a new row with a negative total equal to the amount of a previous order. The sales people were not always thorough and often gave a new order number, or didn’t even list the product number in the correction.
I would like to delete all rows with a negative total, along with their matching (or any other with the same total) positive total row.
My first thought was to simply delete all negative total rows and any positive rows that have the opposite total of them. However, since multiple positive orders exist for many negative orders, this leads to lots of mistakenly deleted positive rows.
How can I delete all rows with a negative total, along with one row for each that has the inverse total?
depending on how much data there is, I would just do it the brute force way.
select all the negative total rows into a temp table
use a cursor to go through each row, then query the database for a single match (using maybe max() on a timestamp, order number, or whatever primary key you might have. Delete that one “matching” row.
then delete all the negative rows
No doubt you can use a subquery and do it in one statement, but by the time I figured it out and tested it, I would have the job done using the above 🙂