De-Duping Data with Ranking Functions

One common problem encountered with imported data is unexpected duplicate data rows, especially if the data is being consolidated from multiple sources. In previous versions of SQL Server, de-duping the data often involved the use of cursors and temp tables. Since the introduction of the ROW_NUMBER ranking function and common table expressions in SQL Server 2005, you can de-dupe data with a single statement.

To demonstrate this approach, Listing 45.38 shows how to create an authors_import table and populate it with some duplicate rows.

LISTING 45.38 Script to Create and Populate the authors_import Table

USE AdventureWorks2012GOCREATE TABLE dbo.authors_import(    au_id char(11) ...

Get Microsoft® SQL Server 2012 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.