I have a table where unique records can be identified using a concatenation of ORDER and ITEM, like so:
| ORDER | ITEM |
|---|---|
| 1234 | 001 |
| 1234 | 002 |
| 1235 | 001 |
| 1235 | 002 |
| 1235 | 003 |
| 1236 | 001 |
| 1237 | 001 |
| ... | ... |
The table currently has duplicates of each unique combination, like so:
| ORDER | ITEM |
|---|---|
| 1234 | 001 |
| 1234 | 002 |
| 1235 | 001 |
| 1235 | 002 |
| 1235 | 003 |
| 1236 | 001 |
| 1237 | 001 |
| 1234 | 001 |
| 1234 | 002 |
| 1235 | 001 |
| 1235 | 002 |
| 1235 | 003 |
| 1236 | 001 |
| 1237 | 001 |
| ... | ... |
I'm using the following subquery to select all duplicate rows:
SELECT * FROM (
SELECT order + item AS ID,
Row_Number() OVER(PARTITION BY order, item ORDER BY order) as CN
FROM [schema].[table]
)
AS Q WHERE Q.CN > 1
With the result:
| ID | CN |
|---|---|
| 1234001 | 2 |
| 1234001 | 3 |
| 1234001 | 4 |
| 1234002 | 2 |
| 1234002 | 3 |
| 1234002 | 4 |
| 1235001 | 2 |
| 1235001 | 3 |
| 1235001 | 4 |
| ... | ... |
Forgive me if this is a very simple question, but is there a simple modification to turn the above subquery into a delete statement and remove every record it currentlyt selects (i.e. with CN > 1)?