Busycontacts find duplicates5/1/2023 ![]() Now lets remove the duplicates/triplicates in one query in an efficient way using Row_Number() Over() with the Partition By clause. Here is the result set that shows the duplicates and triplicates:Įmp_Name Company Join_Date Resigned_Date RowNumber Select a.Emp_Name, a.Company, a.Join_Date, a.Resigned_Date, a.RowNumber So let us add a WHERE clause to the query above and execute it to get the actual duplicates and triplicates: Similarly, the fifth row with RowNumber value of 3 and the fourth row with RowNumber value of 2 are triplicate and duplicate respectively of the third row with RowNumber value of 1. ![]() For example, the second row in the result set above with RowNumber 2 is a duplicate of the first row with RowNumber 1. Wherever the column RowNumber is greater than 1 in the result set above, it is a duplicate row. The results: Emp_Name Company Join_Date Resigned_Date RowNumber ,ROW_NUMBER() over (partition by Emp_Name, Company, Join_Date In essence, the columns in the Partition By clause are being grouped together as per the Partition BY clause and then ordered using the Order By clause: When a new combination of Emp_Name, Company, Join_Date and Resigned_Date columns is encountered, that set is treated as a new partition and the RowNumber starts from 1 again thanks to the Partition By clause. ![]() The subsequent occurrences of the same combination of data are being allocated RowNumber of 2, 3 and so on. The first occurrence of this combination of columns is being allocated a RowNumber of 1. The Row_Number() Over() function is looking for rows with the same values of Emp_Name, Company, Join_Date and Resigned_Date columns in the Emp_Details table. The remaining are duplicates or triplicates as we see from the Occurrences column above.Ī better way of seeing the duplicates & triplicates is the query below where Row_Number() Over() has been used with the Partition By clause. The results: Emp_Name Company Join_Date Resigned_Date OccurrencesĪlthough the table has twelve rows, only seven are good ones as seen in the result set above. Group by Emp_Name, Company, Join_Date, Resigned_Date Please note that we are looking for an exact replica of a row in order ot call it a duplicate in this example: Now, let's see the duplicates and triplicates using the old method where we count the records and use Group By. Insert into Emp_Details (Emp_Name, Company, Join_Date, Resigned_Date) Ensure that you insert at least one duplicate and one triplicate for an easier understanding of the help that the Row_Number() Over() feature offers us: Insert some sample values into this table using the code below. Please note that the code in this article is compatible with both SQL Server 20. A confluence of derived tables and the Row_Number() Over() function in SQL Server 20 makes this elimination process simple. In SQL Server 2000, a program to eliminate duplicates used to be a bit long, involving self-joins, temporary tables, and identity columns. The Row_Number() Over(Partition By.Order by.) feature in Microsoft SQL Server 20 can be used efficiently for eliminating such duplicates. Often we come across situations where duplicate rows exist in a table, and a need arises to eliminate the duplicates.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |