Delete duplicate records from SQL Server 2012 table with identity -


i trying replicate scenario need delete duplicate rows table except one. rows have unique identity column.

for making things easier, created small test table student , script below.

create table student  (     id int,     rollno int,     name varchar(50),     course varchar(50) ) go  insert student values(1,1335592,'john','biology') insert student values(2,1335592,'john','biology') insert student values(3,1335592,'john','biology') insert student values(4,1335592,'john','biology') insert student values(5,1335593,'peter','biology') insert student values(6,1335593,'peter','biology') insert student values(7,1335593,'peter','biology') go  select * student 

this generate table below.

id  rollno  name    course 1   1335592 john    biology 2   1335592 john    biology 3   1335592 john    biology 4   1335592 john    biology 5   1335593 peter   biology 6   1335593 peter   biology 7   1335593 peter   biology 

i keep records id '1' , '5' in result set , delete else. there way this?.

all appreciated.

thanks shammas

it simple query

delete student  id not in (select min(id)                   student                   group rollno, name, course)  

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -