Feb 13 2024 03:15 PM
Hi guys,
I need help creating a delete query...
The table has 4 relevant fields:
ComputerName (nvarchar)
LastContact (datetime)
guid (nvarchar)
When something changes on the computers, the guid value changes so I can have multiple records for the same ComputerName with different guid values. After I review the changes, I want to delete the older records (based on LastContact)
Example:
PLI7285.COMPA.LOCAL 2024-02-07 12:47:48.747 241ca249-2302-4c70-a789-a68580ab473d
PLI7285.COMPA.LOCAL 2024-02-13 09:41:03.727 66f574d1-4d0c-49ca-9458-28834a9db927
I need to delete that first row.
Any suggestion? Thanks.
bye
Dario
Feb 13 2024 09:59 PM
Feb 14 2024 02:59 AM
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
Feb 16 2024 01:59 PM
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.
Feb 23 2024 12:04 PM
Mar 04 2024 10:18 AM