Forum Discussion
Help with delete query
insert into AMP VALUES ('PLI10161.COMPA.LOCAL','2024-02-14 11:50:40.807','9530c214-9aa5-4a27-85d7-8f9488237253','172.30.1.126')
insert into AMP VALUES ('PLI10161.COMPA.LOCAL','2024-02-08 13:30:01.403','e34a0c47-b2dc-4562-8475-ece13ae729c3','192.168.26.91')
I have to delete the "older" (based on LastContact") record with same ComputerName
I was able to adapt a query I found online:
;WITH ToDelete AS (
SELECT ROW_NUMBER() OVER (PARTITION BY ComputerName
ORDER BY LastContact DESC) AS rn
FROM AMP
)
DELETE FROM ToDelete
WHERE rn > 1
But I can't manage to see what records I'm going to delete...
Bye,
Dario
You could try to just do something like this to see what will happen:
;WITH ToDelete AS (
SELECT ROW_NUMBER() OVER (PARTITION BY ComputerName
ORDER BY LastContact DESC) AS rn
FROM AMP
)
SELECT *
--DELETE
FROM ToDelete
WHERE rn > 1
This would let you see what was about to happen prior to running. Then you could just replace the select * with the commented Delete.
- dariopalermo1976Feb 23, 2024Copper Contributor
- Cheef87Mar 04, 2024Copper ContributorSorry just seeing this. You just need to include additional columns in your CTE to see more about the rows that are being deleted.