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.

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Removing Duplicates

Author  Topic 

ncurran217
Starting Member

23 Posts

Posted - 2012-07-18 : 08:53:23
I have a mailing list and have duplicates in the table. I have created this query:


SELECT DISTINCT FIRST, LAST, ADDRESS, CITY, STATE, ZIP, YEAR, MAKE, MODEL, COUNT(*) AS Count
INTO Duplicate_Names
FROM Namestest
GROUP BY FIRST, LAST, ADDRESS, CITY, STATE, ZIP, YEAR, MAKE, MODEL
HAVING (COUNT(*) > 1)

Delete From Namestest
WHERE FIRST, LAST, ADDRESS, CITY, STATE, ZIP, YEAR, MAKE, MODEL
IN (Select FIRST, LAST, ADDRESS, CITY, STATE, ZIP, YEAR, MAKE, MODEL
From Duplicate_Names)

Insert Namestest
Select *
From Duplicate_Names

Drop Table Duplicate_Names


But I am getting this error:

Msg 4145, Level 15, State 1, Line 8
An expression of non-boolean type specified in a context where a condition is expected, near ','.

I do not know what this means or where it is going wrong or how to fix it. Can someone help me. Thanks in advance!

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-18 : 08:59:42
SELECT FIRST, LAST, ADDRESS, CITY, STATE, ZIP, YEAR, MAKE, MODEL, COUNT(*) AS Count
INTO Duplicate_Names
FROM Namestest
GROUP BY FIRST, LAST, ADDRESS, CITY, STATE, ZIP, YEAR, MAKE, MODEL
HAVING (COUNT(*) > 1)

Delete Namestest
from Namestest t
join Duplicate_Names t2
on t.FIRST = t2.FIRST
and t.LAST = t2.LAST
and t.ADDRESS = t2.ADDRESS
and t.CITY = t2.CITY
and t.STATE = t2.STATE
and t.ZIP = t2.ZIP
and t.YEAR = t2.YEAR
and t.MAKE = t2.MAKE
and t.MODEL = t2.MODEL

Insert Namestest
Select *
From Duplicate_Names


notice the removeal of the distinct in the first query - it doesn't do anything.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-18 : 09:03:03
Your Delete Statement is malformed. This would probably work

DELETE n
FROM
NamesTest AS n
JOIN Duplicate_Names AS dn ON
dn.[FIRST] = n.[FIRST]
AND dn.LAST = n.LAST
AND dn.ADDRESS = n.ADDRESS
AND dn.CITY = n.CITY
AND dn.STATE = n.STATE
AND dn.ZIP = n.ZIP
AND dn.YEAR = n.YEAR
AND dn.MAKE = n.MAKE
AND dn.MODEL = n.MODEL

However it is pretty damn messy. No Primary key column on your table to use instead?

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-18 : 09:06:48
Nigel -- think you posted a typo. Not sure if your original code would work or not (not deleting from alias)
quote:
Originally posted by nigelrivett
Delete Namestest t
from Namestest t
join Duplicate_Names t2
on t.FIRST = t2.FIRST
and t.LAST = t2.LAST
and t.ADDRESS = t2.ADDRESS
and t.CITY = t2.CITY
and t.STATE = t2.STATE
and t.ZIP = t2.ZIP
and t.YEAR = t2.YEAR
and t.MAKE = t2.MAKE
and t.MODEL = t2.MODEL
....



Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

ncurran217
Starting Member

23 Posts

Posted - 2012-07-18 : 09:17:03
Thank you guys. I thought this looked different it was from an example off of micorsofts website, so I think it might of been from SQL 2005 coding.

As for the primary key, would I have to copy the table rows to a new table to get a primary key into the table?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-18 : 09:30:36
you could add a column with an identity value if you just wanted a surrogate pk.

So you don't have a primary key at the moment? It makes removing dups much easier.. If you had a UNIQUE CONSTRAINT over the columns in question then you could never get dups at all in the first place...........

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2012-07-18 : 09:33:23
quote:
Originally posted by Transact Charlie

Nigel -- think you posted a typo. Not sure if your original code would work or not (not deleting from alias)
quote:
Originally posted by nigelrivett
Delete Namestest t
from Namestest t
join Duplicate_Names t2
on t.FIRST = t2.FIRST
and t.LAST = t2.LAST
and t.ADDRESS = t2.ADDRESS
and t.CITY = t2.CITY
and t.STATE = t2.STATE
and t.ZIP = t2.ZIP
and t.YEAR = t2.YEAR
and t.MAKE = t2.MAKE
and t.MODEL = t2.MODEL
....



Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/



Nope - that should work ok.
Only need to use the alias if the table is referenced more than once in the query otherwise it's more readable to use the original table name rather than the alias.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

ncurran217
Starting Member

23 Posts

Posted - 2012-07-18 : 09:34:55
Ok so now when I ran the code the first two sets of code worked fine. But now when it tries to put the uniques back into the original table it cannot input them because I did not include all the columns in the first part. I have three columns I left out, LISTCODE, CUSTID and PROMO. LISTCODE could possibly be different in the duplicates, but CUSTID will be different (almost a primary key I would think), but I do not care if those are different I go as a duplicate based on name, address and vehicle. So how would this code work or how would I remove the duplicates as well include those other three columns?
Go to Top of Page

ncurran217
Starting Member

23 Posts

Posted - 2012-07-18 : 09:38:18
quote:
Originally posted by Transact Charlie

you could add a column with an identity value if you just wanted a surrogate pk.

So you don't have a primary key at the moment? It makes removing dups much easier.. If you had a UNIQUE CONSTRAINT over the columns in question then you could never get dups at all in the first place...........



No I do not have a primary key or constraint. And I also consider a duplicate in this case as if the Name, Address and Vehicle are the same. So how would this work?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-18 : 09:54:19
quote:
Originally posted by nigelrivett

quote:
Originally posted by Transact Charlie

Nigel -- think you posted a typo. Not sure if your original code
....
....


Nope - that should work ok.
Only need to use the alias if the table is referenced more than once in the query otherwise it's more readable to use the original table name rather than the alias.
....


Cheers! Guess I never thought about it like that.

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-18 : 10:01:31
quote:
Originally posted by ncurran217

Ok so now when I ran the code the first two sets of code worked fine. But now when it tries to put the uniques back into the original table it cannot input them because I did not include all the columns in the first part. I have three columns I left out, LISTCODE, CUSTID and PROMO. LISTCODE could possibly be different in the duplicates, but CUSTID will be different (almost a primary key I would think), but I do not care if those are different I go as a duplicate based on name, address and vehicle. So how would this code work or how would I remove the duplicates as well include those other three columns?


Well you'll have to pick one to keep.

Lets try something else! How about trying this

BEGIN TRAN


; WITH rankedNamesTest AS (
SELECT *
, ROW_NUMBER() OVER (
PARTITION BY FIRST, LAST, ADDRESS, CITY, STATE, ZIP, YEAR, MAKE, MODEL
ORDER BY CUSTID
)
AS [RowID]
FROM
NamesTest
)
DELETE r
FROM rankedNamesTest AS r
WHERE r.[RowID] > 1

SELECT @@ROWCOUNT AS [Dups_Deleted]

ROLLBACK

This is a different approach. Instead just delete the dups directly and keep 1 record for each one (ordered by whatever the [CUSTID] value is.

Check things look good and the remove the transaction

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

ncurran217
Starting Member

23 Posts

Posted - 2012-07-18 : 10:28:05
Ok, that code detects the correct amount of dupes. But doesnt seem to delete them. Am I missing something?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-18 : 11:17:38
did you run it without the transaction?

BEGIN TRAN starts a transaction

ROLLBACK rolls back any modification made in that transaction.

If the number is right then change the ROLLBACK statement into a COMMIT statement.

does that help?

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page

ncurran217
Starting Member

23 Posts

Posted - 2012-07-18 : 11:30:02
Yes that works. Sorry I have never done a statement with Transaction and Rollback. Thank you a lot with the help!
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2012-07-18 : 11:51:38
welcome

Transact Charlie
Msg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.
http://nosqlsolution.blogspot.co.uk/
Go to Top of Page
   

- Advertisement -