Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to segregate the duplicate and distinct rows from source table to separate target tables?
source table:

COL1 COL2 COL3
a b c
x y z
a b c
r f u
a b c
v f r
v f r
Target Table 1: Table containing all the unique rows
COL1 COL2 COL3
a b c
x y z
r f u
v f r

Target Table 2: Table containing all the duplicate rows

COL1 COL2 COL3
a b c
a b c
v f r

these is done now i want to link target table 1 to target table 2
Posted
Updated 29-Apr-14 20:20pm
v4
Comments
King Fisher 28-Apr-14 8:26am    
can you Show your Table structure Simply and what you have to do?

 
Share this answer
 
v2
SQL
SELECT [YourColumnName]
FROM
(
    SELECT Row_Number() over (partion by YourColumnName order by YourColumnName) as counter, YourColumnName
    FROM [yourtable]
) as doubles
where doubles.counter > 1
 
Share this answer
 
Comments
aparnaChandras 22-Apr-14 7:32am    
both are worked thank you
aparnaChandras 22-Apr-14 7:34am    
now i want to create a table with id of first occurance in duplicate record in one column and id of other occurances of duplicates in other column
Herman<T>.Instance 22-Apr-14 7:39am    
where doubles.counter = 1
aparnaChandras 22-Apr-14 7:48am    
not getting it exactly
Herman<T>.Instance 22-Apr-14 10:09am    
what you don't understand?
Hi,
Use following query for your need and let me know if it helps you.
SQL
SELECT [YourColumnName],
RANK() OVER (
ORDER BY COUNT(*)) AS 'Rank'
FROM [YourTableName]
GROUP BY [YourColumnName]

Thanks,
Hitesh Varde
 
Share this answer
 
Comments
Herman<T>.Instance 22-Apr-14 3:00am    
RANK() leads to the same number when having equal result in the row
try this count duplicate values

SQL
SELECT [ColumnName],
DENSE_RANK() OVER (
ORDER BY COUNT(*)) AS 'Count'
FROM [TableName]
GROUP BY [ColumnName]
 
Share this answer
 
v2
Comments
aparnaChandras 25-Apr-14 5:59am    
it will give only exact match columns not likely.
for ex. i have one column like Engine Type ,it has value like ME9,MC4,ME9.1, here ME9 and ME9.1 are same but it'll not consider as per above query

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900