Issue deleting duplicate records in SQL so I seem to be having a moment and can't figure out why certain dups in a table are not getting deleted. I have a test table called QUERY that has names, addresses, DOB's, phones, etc. I am looking to delete the dups, but want to keep the most recent record (preferable but the below code doesn't represent that) where the phone is not empty. My code below just isn't working always giving 0 results. An example of a row would be:
++
|   first,last,DOB,address,city,state,phonenumber,validitydate   |
++
| steve,smith,19710922, 123 Here St, Miami, FL,9545551212,201902 |
| steve,smith,19710922, 123 Here St, Miami, FL,,202009|
| steve,smith,19710922, 123 Here St, Miami, FL,9545551212,201802 |
++

WITH Records AS
(  
SELECT lastname, firstname, address, state, dateofbirth, phonenumber
    ,ROW_NUMBER() OVER (PARTITION BY lastname, firstname, address, state, dateofbirth, phonenumber order by validitydate) AS RecordInstance
    ,ROW_NUMBER() OVER (PARTITION BY lastname, firstname, address, state, dateofbirth, phonenumber order by
    CASE
WHEN phonenumber ='' THEN 0
WHEN phonenumber IS NOT NULL THEN 1
Else 0
END) as [ToInclude]
FROM query 
)
    
delete
FROM records
WHERE
    RecordInstance > 1
    and ToInclude = 0

Anyone see anything I am doing wrong?? Thanks in advance

I think here is what you want to do :
SELECT 
    RANK() OVER (PARTITION BY lastname, firstname, address, state, dateofbirth, phonenumber order by validitydate) AS RecordInstance
   ,COUNT(*) OVER (PARTITION BY lastname, firstname, address, state, dateofbirth, phonenumber) DUPS
FROM QUERY
WHERE DUPS > 2 
AND RecordInstance = 1
AND phonenumber <> '' -- maybe? if you really don't want to delete duplicates with no phone number


If your required result is to delete all the duplicate records where phone number is not null or empty then you can try below query:
WITH Records AS
(  
SELECT lastname, firstname, address, state, dateofbirth, phonenumber
    ,ROW_NUMBER() OVER (PARTITION BY lastname, firstname, address, state, dateofbirth, phonenumber order by validitydate) AS RecordInstance
    
FROM query where phonenumber<>'' and phonenumber is not null
)
    
delete
FROM records
WHERE
    RecordInstance > 1


Try
RecordInstance = ROW_NUMBER()   OVER(PARTITION BY LastName, FirstName,  Address, City, DateOfBirth ORDER BY PhoneNumber DESC, ValidityDate)