I have a table that consists information about status of each customer every day. However, due to space saving, I would like to keep that information only if something has changed. More specifically:
ID | Customer ID | Date | Status |
---|---|---|---|
1 | 1 | 2024-02-01 | ACTIVATED |
2 | 1 | 2024-02-02 | ACTIVATED |
3 | 1 | 2024-02-03 | ACTIVATED |
4 | 1 | 2024-02-04 | DEACTIVATED |
5 | 2 | 2024-02-01 | ACTIVATED |
6 | 2 | 2024-02-02 | ACTIVATED |
7 | 3 | 2024-02-03 | ACTIVATED |
8 | 4 | 2024-02-04 | ACTIVATED |
9 | 5 | 2024-02-05 | DEACTIVATED |
How do I remove (DELETE) rows 2,3,6,7,8 and keep 1,4,5,9 with SQL?
Tried ranking, but didn’t get there.
3
Answers
Based on this your delete would be like:
DBFiddle demo
When I do read your question, I think you want to keep all rows where R=1
The DELETE statement could be:
see: DBFIDDLE
Is there a logic behind which row to delete?
To answer the question:
"How do I remove (DELETE) rows 2,3,6,7,8 and keep 1,4,5,9 with SQL?"
That is probably not what you were asking. We could only gues what you realy need to do. Here is one gues (though not deleting all IDs as in question) – maybe you want to delete multiple activations of customer id and keep just the first one. Below is code deleting IDs 2, 3, 6 as those customers were already activated.