Let’s assume that data has been entered in a table and now you want to find if a table column have duplicated values.
Here is an SQL script to join data from one table for comparison purpose. The table must have a primary key column (unique column) and the second column for comparison.
Here is a sample script:
select distinct(s.DWDOCID) as DOCID, 'Y' AS AIXFlag from [dbo].[SAMPLEEN] s inner join [dbo].[SAMPLEEN] s2 on s.CUSTOMER_NUMBER = s2.CUSTOMER_NUMBER where s.DWDOCID <> s2.DWDOCID
– DWDOCID is the primary key column
– AIXFlag, is just a fixed value (optional)
– SAMPLEEN is the table name
– CUSTOMER_NUMBER is the column where I am searching for duplicated values
– where s.DWDOCID <> s2.DWDOCID … the reason we include this statement is because we don’t want the value of the same row
In brief description, here I am looking for duplicated customer_number. If the query returns a value it means we have duplicated customer_number and using the DOCID column as primary key column.