Skip to main content

DELETE DUPLICATE ROWS FROM A TABLE

DECLARE @COUNT INT
DECLARE @TOTAL INT
DECLARE @POST VARCHAR(100)
DECLARE @TBL TABLE
(
ID INT IDENTITY,
Post VARCHAR(100),
TOTAL INT


)
INSERT INTO @TBL

SELECT Post, count(*) as [count]
FROM tblemp
GROUP BY Post
HAVING count(*) > 1

SET @COUNT=(SELECT COUNT(*) FROM @TBL)

WHILE @COUNT>0
BEGIN
SET @POST=(SELECT POST FROM @TBL WHERE ID=@COUNT)
SET @TOTAL=(SELECT TOTAL FROM @TBL WHERE ID=@COUNT)
DELETE TOP(@TOTAL-1) FROM tblemp WHERE POST=@POST

SET @COUNT=@COUNT-1
END



Other Way//////////////////////////



How to Delete duplicate rows from a table?
http://www.cryer.co.uk/brian/sql/sql_delete_duplicates.htm
Create table tbltest(Id int identity,Name varchar(50),Age int)
//Insert Duplicate Record in the table
insert into tbltest values('Amandeep',25)

//Select * from tbltest

1 Manpreet 24
7 Amandeep 25
8 Amandeep 25
4 Amandeep 25
9 Amandeep 25
10 Manpreet 24
11 Manpreet 24
12 Manpreet 24

delete T1
from tbltest T1, tbltest T2
where T1.Name = T2.Name
and T1.Age = T2.Age
and T1.Id > T2.Id

Run the above Query
//Select * from tbltest
1 Manpreet 24
4 Amandeep 25

Comments

Popular posts from this blog

URL Rewritting

http://www.simple-talk.com/dotnet/asp.net/a-complete-url-rewriting-solution-for-asp.net-2.0/ http://msdn.microsoft.com/en-us/library/ms972974.aspx URL Rewriting in ASP.NET Summary: Examines how to perform dynamic URL rewriting with Microsoft ASP.NET. URL rewriting is the process of intercepting an incoming Web request and automatically redirecting it to a different URL. Discusses the various techniques for implementing URL rewriting, and examines real-world scenarios of URL rewriting. (31 printed pages) Download the source code for this article. Contents Introduction Common Uses of URL Rewriting What Happens When a Request Reaches IIS Implementing URL Rewriting Building a URL Rewriting Engine Performing Simple URL Rewriting with the URL Rewriting Engine Creating Truly "Hackable" URLs Conclusion Related Books Introduction Take a moment to look at some of the URLs on your website. Do you find URLs like http://yoursite.com/info/dispEmployeeInfo.aspx?EmpID=459-099&type=summ...

SEND A PDF FILE AS AN ATTACHEMENT OF MAIL!

System.Net.Mail.MailMessage m1 = new System.Net.Mail.MailMessage(); m1.From = "manpreet@gmail.com" m1.Subject = "Test mail "; m1.Body = str.ToString(); m1.IsBodyHtml = true; m1.To.Add("jasdeep@gmail.com"); m1.CC.Add("jasdeep123@gmail.com"); m1.Attachments.Add(new Attachment(strserverpath + @"\pdf\" + PdfFileName)); smtp.Send(m1);

Sql Server Tips

IF WE WANT TO SELECT  TOP N ROWS  FROM A TABLE ,  WE USE THE FETCH NEXT  SELECT    * FROM  table1   ---//First m rows ignore first n rows ORDER BY id OFFSET n ROWS FETCH NEXT m ROWS ONLY if n=0, m=10 then first  10 rows if n=10, m=10 then first  10 rows start from 11th row Row_number()   SELECT  ROW_NUMBER() OVER(order by db.id) Insertion Simultaneouly in temp table with output clause DECLARE @table1 table (   Id int,   name nvarchar(50) ); DECLARE @table2 table (  Id int,  name nvarchar(50) ); INSERT INTO @table2 OUTPUT INSERTED.*   INTO @table1   select top 10 id, name  from FinalTable SELECT * FROM @table2; SELECT * FROM @table1;