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.
| Author |
Topic |
|
Rasta Pickles
Posting Yak Master
174 Posts |
Posted - 2012-09-01 : 11:20:26
|
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 ofWHERE CRNumber NOT IN (SELECT CRNumber FROM orders) but ended up confusing myself Thanks in advance as always. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-01 : 12:37:20
|
| [code]FROM worklogs wWHERE NOT EXISTS (SELECT 1 FROM orders WHERE CRNumber = w.CRNumber)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Rasta Pickles
Posting Yak Master
174 Posts |
Posted - 2012-09-01 : 12:52:13
|
Thank you, am I anywhere close with this?;WITH cte AS(SELECT ROW_NUMBER() OVER (PARTITION BY worklogid ORDER BY CRNumber) AS RNFROM worklogs wWHERE NOT EXISTS (SELECT 1 FROM orders WHERE CRNumber = w.CRNumber) I'm not "feeling it" as the youth of Britain say...... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-01 : 12:54:11
|
| sorry why do you need a CTE here? you can have this in select itself------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Rasta Pickles
Posting Yak Master
174 Posts |
Posted - 2012-09-01 : 13:03:16
|
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 wWHERE NOT EXISTS (SELECT 1 FROM orders WHERE CRNumber = w.CRNumber) ? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-01 : 13:04:18
|
| yep...as per your requirement------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Rasta Pickles
Posting Yak Master
174 Posts |
Posted - 2012-09-01 : 14:54:36
|
Outrageously simple.I will try it on Monday |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-01 : 15:03:27
|
| ok...nplet me know how you got on!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|