I have a table in Microsoft Access with, among others, the fields (UN, PSN, Class, ClassAll, PG, priority). I need to delete duplicate records based on the fields (UN, PSN, Class, ClassAll, PG) but retain only the ones with the lowest priority value in field priority I tried the following:
DELETE FROM tbl
WHERE (UN, PSN, Class, ClassAll, PG, priority) NOT IN (
SELECT UN, PSN, Class, ClassAll, PG, MIN(priority) AS MinPriority
FROM tbl
GROUP BY UN, PSN, Class, ClassAll, PG
);
However, this results in an error:
Query must have at least one destination field
Below a small example of the data. the bold marked values are considered duplicates because the values are the same in the fields (UN, PSN, Class, ClassAll, PG) In this example the records with IDs 6589 and 17 should be deleted and the one with the lowest priority value (ID 120) shall retain
SAMPLE DATA
ID | UN | PSN | Class | classAll | PG | Priority |
---|---|---|---|---|---|---|
120 | 1006 | AUDI | 2.2 | 2.2 | A | 1 |
2 | 1950 | VW | 2.1 | 2.2,8 | A | 11 |
3 | 1950 | VW | 2.1 | 2.2,6.1 | B | 10 |
4 | 1950 | VW | 2.1 | 2.2,6.1,8 | B | 12 |
6589 | 1006 | AUDI | 2.2 | 2.2 | A | 2 |
6 | 1950 | PEUGEOT | 2.2 | 2.2 | B | 18 |
17 | 1006 | AUDI | 2.2 | 2.2 | A | 9 |
DESIRED OUTCOME
ID | UN | PSN | Class | classAll | PG | Priority |
---|---|---|---|---|---|---|
120 | 1006 | AUDI | 2.2 | 2.2 | A | 1 |
2 | 1950 | VW | 2.1 | 2.2,8 | A | 11 |
3 | 1950 | VW | 2.1 | 2.2,6.1 | B | 10 |
4 | 1950 | VW | 2.1 | 2.2,6.1,8 | B | 12 |
6 | 1950 | PEUGEOT | 2.2 | 2.2 | B | 18 |
What would be the proper way to achive this?
Try with:
DELETE *
FROM tbl As T1
WHERE T1.priority NOT IN (
SELECT Top 1 T2.priority
FROM tbl As T2
WHERE T1.UN = T2.UN And T1.PSN = T2.PSN And T1.Class = T2.Class And T1.ClassAll = T2.ClassAll
ORDER BY Priority Asc);
Result: