Question : SQL - list only  those records where key fields repeat

I have a log table that tracks file submissions with all relevant details. The table has a normal incremental primary key.

I need to list only those individual records for files that have been submitted more than once, based on specific columns that tell me if a record is a repeat.

How can I do this?

 
 
Table description
323693
 

Answer : SQL - list only  those records where key fields repeat

>> specific columns that tell me if a record is a repeat.

So, what are those specific columns ?  What version of SQL Server ?

Few thoughts come to mind...  Lets assume (say) orig_filename, group, type are the three columns that uniquely identify your file for submission

-- thought 1 - show just the additional instances - put the columns actually needed to uniquely identify in the "partition by" part  (SQL 2005, SQL 2008)

select *
from (select row_number() over (partition by orig_filename, [group], [type] order by submitid) as rn, * from csmsadmin.submissions) s
where s.rn > 1

-- thought 2 - use the count to identify orig_filename, includes all instances not just the dupes (any SQL Server)

select *
from csmsadmin.submissions s
where 1 < (select count(*) from csmsadmin.submissions sc where sc.orig_filename = s.orig_filename and sc.[group] = s.[group] and sc.[type] = s.[type])

-- thought 3 - use a join - particularly if there are indexes to use (any SQL Server)

select s.*
from csmsadmin.submissions s
inner join csmsadmin.submissions sc on sc.orig_filename = s.orig_filename and sc.[group] = s.[group] and sc.[type] = s.[type] and sc.submitid <> s.submitid


There are other methods as well, depends a little bit on what we can and cant use in terms of columns and version
Random Solutions  
 
programming4us programming4us