[TIL] MSSQL delete duplicate rows

Funnystyle·2021년 2월 17일
0

MSSQL 에 중복행이 들어 있을 때, updatedelete가 되지 않는 경우가 있다.
이럴 때 아래 구문을 응용하여 삭제하면 한 줄만 남기게 된다.

WITH cte AS (
    SELECT 
        contact_id, 
        first_name, 
        last_name, 
        email, 
        ROW_NUMBER() OVER (
            PARTITION BY 
                first_name, 
                last_name, 
                email
            ORDER BY 
                first_name, 
                last_name, 
                email
        ) row_num
     FROM 
        sales.contacts
)
DELETE FROM cte
WHERE row_num > 1;

출처: https://www.sqlservertutorial.net/sql-server-basics/delete-duplicates-sql-server/

profile
polyglot

0개의 댓글