1

I would like to delete the entire row for ALL of the duplicates this query finds. I do not wish to leave a single record. How do I add the delete row syntax to this complicated query?

SELECT u.id, u.School, u.Mascot, u.City, u.State
FROM TABLE u
INNER JOIN
(
    SELECT Mascot, City, State, COUNT(*)
    FROM tablename
    GROUP BY Mascot, City, State
    HAVING COUNT(*) > 1
) temp
    ON temp.Mascot = u.Mascot AND
       temp.City = u.City AND
       temp.State = u.State
ORDER BY
    Mascot, City, State;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Wyatt Jackson
  • 303
  • 1
  • 2
  • 11

1 Answers1

0

Try grouping and then retaining only those groups having a count of 1:

SELECT
    u.id, u.School, u.Mascot, u.City, u.State
FROM TABLE u
INNER JOIN
(
    SELECT Mascot, City, State, COUNT(*)
    FROM tablename
    GROUP BY Mascot, City, State
    HAVING COUNT(*) > 1
) temp
    ON temp.Mascot = u.Mascot AND
       temp.City   = u.City   AND
       temp.State  = u.State
GROUP BY
    u.id, u.School, u.Mascot, u.City, u.State
HAVING
    COUNT(*) = 1
ORDER BY
    Mascot, City, State;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360