SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Another "delete duplicate records" question
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 09/01/2012 :  11:20:26  Show Profile  Reply with Quote
I've just about got my head around using a CTE to delete duplicate records but can the syntax be extended to do a bit more?

I will explain

Assume I have a table called worklogs and one of the fields is named worklogid. I know there are going to be duplicate entries in this field but I have to be a bit clever in making sure I delete the right ones.

One of the other fields in this table is called CRNumber.

Now, here's the messy bit (to my novice brain anyway).....I have another table called orders and one of the fields is named CRNumber.

Is it possible to delete duplicate records from the worklogs table where the CRNumber field does NOT appear in the orders table (i.e I want to keep only records where there is an entry in the orders table)?

I started going down the route of


WHERE CRNumber NOT IN (SELECT CRNumber FROM orders)


but ended up confusing myself

Thanks in advance as always.

visakh16
Very Important crosS Applying yaK Herder

India
47037 Posts

Posted - 09/01/2012 :  12:37:20  Show Profile  Reply with Quote

FROM worklogs w
WHERE NOT EXISTS (SELECT 1 FROM orders WHERE CRNumber = w.CRNumber)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 09/01/2012 :  12:52:13  Show Profile  Reply with Quote
Thank you, am I anywhere close with this?


;WITH cte AS
(SELECT ROW_NUMBER() OVER (PARTITION BY worklogid ORDER BY CRNumber) AS RN
FROM worklogs w
WHERE NOT EXISTS (SELECT 1 FROM orders WHERE CRNumber = w.CRNumber)


I'm not "feeling it" as the youth of Britain say......
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47037 Posts

Posted - 09/01/2012 :  12:54:11  Show Profile  Reply with Quote
sorry why do you need a CTE here? you can have this in select itself

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 09/01/2012 :  13:03:16  Show Profile  Reply with Quote
I'm still learning

My "safe" knowledge is that to delete duplicate rows you use a CTE?

Are you saying it's as simple as


DELETE FROM worklogs w
WHERE NOT EXISTS (SELECT 1 FROM orders WHERE CRNumber = w.CRNumber)


?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47037 Posts

Posted - 09/01/2012 :  13:04:18  Show Profile  Reply with Quote
yep...as per your requirement

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Rasta Pickles
Posting Yak Master

United Kingdom
133 Posts

Posted - 09/01/2012 :  14:54:36  Show Profile  Reply with Quote
Outrageously simple.

I will try it on Monday
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47037 Posts

Posted - 09/01/2012 :  15:03:27  Show Profile  Reply with Quote
ok...np

let me know how you got on!

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000