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_NamesDrop Table Duplicate_Names But I am getting this error:Msg 4145, Level 15, State 1, Line 8An 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 Namestestfrom Namestest tjoin Duplicate_Names t2on t.FIRST = t2.FIRSTand t.LAST = t2.LASTand t.ADDRESS = t2.ADDRESSand t.CITY = t2.CITYand t.STATE = t2.STATEand t.ZIP = t2.ZIPand t.YEAR = t2.YEARand t.MAKE = t2.MAKEand t.MODEL = t2.MODELInsert Namestest Select * From Duplicate_Namesnotice 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. |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-07-18 : 09:03:03
|
Your Delete Statement is malformed. This would probably workDELETE nFROM 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 CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
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 nigelrivettDelete Namestest tfrom Namestest tjoin Duplicate_Names t2on t.FIRST = t2.FIRSTand t.LAST = t2.LASTand t.ADDRESS = t2.ADDRESSand t.CITY = t2.CITYand t.STATE = t2.STATEand t.ZIP = t2.ZIPand t.YEAR = t2.YEARand t.MAKE = t2.MAKEand t.MODEL = t2.MODEL....
Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
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? |
|
|
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 CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
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 nigelrivettDelete Namestest tfrom Namestest tjoin Duplicate_Names t2on t.FIRST = t2.FIRSTand t.LAST = t2.LASTand t.ADDRESS = t2.ADDRESSand t.CITY = t2.CITYand t.STATE = t2.STATEand t.ZIP = t2.ZIPand t.YEAR = t2.YEARand t.MAKE = t2.MAKEand t.MODEL = t2.MODEL....
Transact CharlieMsg 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. |
|
|
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? |
|
|
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? |
|
|
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 CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
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 thisBEGIN 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 rFROM rankedNamesTest AS rWHERE 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 transactionTransact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
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? |
|
|
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 transactionROLLBACK 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 CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
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! |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-07-18 : 11:51:38
|
welcomeTransact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
|