Search code examples
sqlsql-serversql-delete

Going from Select Statement to Delete


I have this query which I found to get duplicate records:

SELECT x.[Constituent Number], x.[First Name], x.[Last or Org Name],x.[Address Line 1], Cnt = Count(*) 
FROM (   
    SELECT SR.[Constituent Number], SR.[First Name], SR.[Last or Org Name], SR.[Address Line 1]
    FROM dbo.['All Salsa Records TL$'] SR

    UNION ALL

    SELECT WD.[Member Number], WD.[First Name], wd.[Last or Org Name], WD.[Address Line 1]
    FROM dbo.['Workling to delete$'] WD
) x
GROUP BY x.[Constituent Number], x.[First Name],x.[Last or Org Name],x.[Address Line 1]
HAVING COUNT(*) > 1

This is between two tables. How would I go about deleting records from the master table to match this queries selected results?

EDIT: I managed to figure it out using this code:

DELETE SR
--SELECT SR.[Constituent Number], SR.[First Name], SR.[Last or Org Name]
FROM dbo.['All Salsa Records TL$'] AS SR
LEFT OUTER JOIN dbo.['Workling to delete$'] WD
    ON WD.[Member Number] = SR.[Constituent Number]
    AND WD.[First Name] = SR.[First Name]
    AND WD.[Last or Org Name] = SR.[Last or Org Name] 
    AND WD.[Address Line 1] = SR.[Address Line 1]
    AND WD.[City]  = SR.[City]
    AND WD.[State] = SR.[State]
WHERE SR.[Constituent Number] = WD.[Member Number]
    AND WD.[First Name] = SR.[First Name]
    AND WD.[Last or Org Name] = SR.[Last or Org Name] 
    AND WD.[Address Line 1] = SR.[Address Line 1]
    AND WD.[City]  = SR.[City]
    AND WD.[State] = SR.[State]

For the nulls, I just used this (and changed accordingly as I had 11 records with null constituents and 26 records with null addresses):

DELETE SR
--SELECT SR.[Constituent Number], SR.[First Name], SR.[Last or Org Name]
FROM dbo.['All Salsa Records TL$'] AS SR
LEFT OUTER JOIN dbo.['Workling to delete$'] WD
    ON WD.[Member Number] = SR.[Constituent Number]
    AND WD.[First Name] = SR.[First Name]
    AND WD.[Last or Org Name] = SR.[Last or Org Name] 
    AND WD.[Address Line 1] IS NULL
    AND WD.[City] IS NULL
    AND WD.[State] IS NULL
WHERE SR.[Constituent Number] = WD.[Member Number]
    AND WD.[First Name] = SR.[First Name]
    AND WD.[Last or Org Name] = SR.[Last or Org Name] 
    AND SR.[Address Line 1] IS NULL
    AND SR.[City] IS NULL
    AND SR.[State] IS NULL

Solution

  • If ['All Salsa Records TL$'] is your master table and [Constituent Number] is the column against which you want to delete rows then :

    with cte as(
    SELECT x.[Constituent Number], x.[First Name], x.[Last or Org Name],x.[Address Line 1], Cnt = Count(*) 
    FROM (   
        SELECT SR.[Constituent Number], SR.[First Name], SR.[Last or Org Name], SR.[Address Line 1]
        FROM dbo.['All Salsa Records TL$'] SR
    
        UNION ALL
    
        SELECT WD.[Member Number], WD.[First Name], wd.[Last or Org Name], WD.[Address Line 1]
        FROM dbo.['Workling to delete$'] WD
    ) x
    GROUP BY x.[Constituent Number], x.[First Name],x.[Last or Org Name],x.[Address Line 1]
    HAVING COUNT(*) > 1)
    delete dbo.['All Salsa Records TL$'] where [Constituent Number]  in (select [Constituent Number] from cte)