{"id":1855,"date":"2013-05-31T11:09:35","date_gmt":"2013-05-31T11:09:35","guid":{"rendered":"http:\/\/www.withdata.com\/ad\/?p=1855"},"modified":"2017-04-20T10:49:11","modified_gmt":"2017-04-20T10:49:11","slug":"remove-duplicate-rows-from-sql-server-table","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/sql-server\/remove-duplicate-rows-from-sql-server-table.html","title":{"rendered":"Remove duplicate rows in SQL Server"},"content":{"rendered":"<p>Assuming no nulls, you GROUP BY the unique columns (eg. col_1, col_2, col_3), and SELECT the MIN (or MAX) Row ID (eg. row_id) as the row to keep. Then, delete everything that didn&#8217;t have a row id:<\/p>\n<pre>DELETE my_table\r\nFROM my_table\r\n\tLEFT OUTER JOIN (\r\n\tSELECT MIN(row_id) as row_id, col_1, col_2, col_3\r\n\tFROM my_table\r\n\tGROUP BY col_1, col_2, col_3\r\n) as keep_rows ON\r\n\tmy_table.row_id = keep_rows.row_id\r\nWHERE\r\n\tkeep_rows.row_id IS NULL<\/pre>\n<p>Use<\/p>\n<pre>CONVERT(uniqueidentifier, MIN(CONVERT(char(36), my_guid_column)))<\/pre>\n<p>instead of MIN(row_id) if you have a GUID instead of an integer.<\/p>\n<p>Relative links:<\/p>\n<h2><a title=\"Permalink to Remove duplicate rows in MySQL\" href=\"https:\/\/www.withdata.com\/blog\/mysql\/remove-duplicate-rows-in-mysql.html\" rel=\"bookmark\">Remove duplicate rows in MySQL<\/a><\/h2>\n<h2><a title=\"Permalink to Remove duplicate rows in Oracle\" href=\"https:\/\/www.withdata.com\/blog\/oracle\/remove-duplicate-rows-in-oracle.html\" rel=\"bookmark\">Remove duplicate rows in Oracle<\/a><\/h2>\n<p>Some SQL Server tools you can try: <a href=\"https:\/\/www.withdata.com\/sql-server\/\"> https:\/\/www.withdata.com\/sql-server\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Assuming no nulls, you GROUP BY the unique columns (eg. col_1, col_2, col_3), and SELECT the MIN (or MAX) Row ID (eg. row_id) as the row to keep. Then, delete everything that didn&#8217;t have a row id: DELETE my_table FROM my_table LEFT OUTER JOIN ( SELECT MIN(row_id) as row_id, col_1, col_2, col_3 FROM my_table GROUP [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[234,1321],"class_list":["post-1855","post","type-post","status-publish","format-standard","hentry","category-sql-server","tag-remove-duplicate-rows","tag-sql-server"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=1855"}],"version-history":[{"count":8,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1855\/revisions"}],"predecessor-version":[{"id":6425,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/1855\/revisions\/6425"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=1855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=1855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=1855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}