Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Find rows with duplicate entries
Message
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01454248
Message ID:
01454258
Views:
28
>>>>I am sure this question has been asked million times but here is a million one:
>>>>
>>>>What SQL Select to use to find all records with duplicate entries? I am trying to add a UNIQUE constraint to a table but get error that duplicate rows exist.
>>>>
>>>>TIA.
>>>
>>>SQL Server 2005 and up
>>>
>>>select * from (select *, 
>>>count(*) over (partition by GroupField) as cntDups from myTable) X where cntDups > 1
>>>
>>>SQL Server 2000 solution
>>>
>>>select T.* from myTable T 
>>>inner join (select GroupField, count(*) as cntDups from myTable group by GroupField having count(*) > 1) X
>>>on T.GroupField = X.GroupField
>>
>>What is "X" in your expression?
>
>X is the alias for derived table. Funny, I explained it today once - it's because I'm lazy I use X.

Thank you. This is what I thought. But I was initially getting an error new ")" and I thought it was the X. But then I noticed that you have changed your expression after you posted it.

Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform