Where you should start?
Try querying all EntryLine
.
SELECT *
FROM EntryLine
If that works, proceed to find the number of duplicates for all EntryLine
.
SELECT LeadManifestID, VINid, count(*)
FROM EntryLine
If that works, try finding a deterministic EntryLineNo
for each group of EntryLine
.
SELECT MIN(EntryLineNo), LeadManifestID, VINid, count(*)
FROM EntryLine
Once that's done, you need to find all rows that are duplicate but don't share the preselected EntryLineNo
.
SELECT *
FROM EntryLine e INNER JOIN (
SELECT MIN(EntryLineNo) AS KeptNo, LeadManifestID, VINid, count(*)
FROM EntryLine
) e2 ON e.VINid = e2.VINid AND e.LeadManifestID = e2.VINid AND e2.KeptNo != e.EntryLineNo
Then, you simply have to update the table
UPDATE
e
SET
toBeDelted = true
FROM
SELECT *
FROM EntryLine e INNER JOIN (
SELECT MIN(EntryLineNo) AS KeptNo, LeadManifestID, VINid, count(*)
FROM EntryLine
) e2 ON e.VINid=e2.VINid AND e.LeadManifestID=e2.VINid AND e2.KeptNo!=e.EntryLineNo
This is a pretty wasteful method though, but it is definitely a good way to start. From then, you could look into ROW_NUMBER
for instance and simplify the process so much.