Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Over () Partition By () Query

Author  Topic 

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-08-10 : 13:12:00
Hi All - I have the following Table:

Serial_Num

123456
123456
123456
123457
123458
123459

I am trying to identify which of these are duplicates, by having the output as follows:

Serial_Num | DUPLICATE

123456 Y
123456 Y
123456 Y
123457 N
123458 N
123459 N


I've used the over by/partition by function, but I need to replace it with a Y/N as described above.

SELECT * FROM

(SELECT Serial_Num, ROW_NUMBER() OVER (PARTITION BY Serial_Num ORDER BY Serial_Num) AS Rn FROM MyTable) T1

Any ideas? Thanks!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-08-10 : 13:45:52
SELECT Serial_Num, CASE WHEN COUNT(*) OVER (PARTITION BY Serial_Num)>1 THEN 'Y' ELSE 'N' END Duplicate FROM MyTable
Go to Top of Page

funk.phenomena
Posting Yak Master

121 Posts

Posted - 2012-08-10 : 15:57:34
Thanks! Works great!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-08-16 : 08:54:52
Also

SELECT Serial_Num,case when count(*)>1 then 'Y' else 'N' end as duplicate from table
group by Serial_Num

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -