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 |
BigKaTooMer
Starting Member
3 Posts |
Posted - 2006-12-11 : 07:02:25
|
Hi there. This is my first post here because I am well and truely stumped and would really appreciate some help!I have a SQL Server table containing customer records.I have hundreds of records where the record's Customer Name is duplicated many times.I do not wish to delete the duplicates, but update the Customer ID field of each duplicate with that of one of the matches.For example:I want to go from:Customer Name Customer ID Bob 1Bob 2James 3James 4James 5Bob 6Betty 7To:Customer Name Customer ID Bob 1Bob 1James 3James 3James 3Bob 1Betty 7Any ideas of the SQL to get here would be greatly appreciated. I can identify the dupes, and count the occurences of them, but not update them to the Customer number of one of them.I hope this is clear enough for someone to help me!Many thanks,WillSQL Newbie! |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-11 : 07:09:42
|
[code]declare @t table( custid int, custname varchar(20))insert @tselect 1, 'Bob' union allselect 2, 'Bob' union allselect 3, 'James' union allselect 4, 'James' union allselect 5, 'James' union allselect 6, 'Bob' union allselect 7, 'Betty'update t1set custid = (select min(custid) from @t t2 where t2.custname = t1.custname)from @t t1select * from @t[/code]Note: I assume that CustID is not an identity column.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
BigKaTooMer
Starting Member
3 Posts |
Posted - 2006-12-11 : 09:26:14
|
Harsh,Thanks for such a swift reply! Much appreciated.Obviously, pasting your SQL as written works perfectly in context and that is just the job I want to do, though I do need to write the result to a table.Howver, when it came to interpreting the result to my specific problem, I have used the SQL you published but I don't think I've got it correctly as it makes no changes.My table is called EIM_CUSTOMERS and I only have one table so far, so the @t and the t1 and t2 variables have thrown me a bit. could you please explain what the refer to? Is t2 a 'create like' table for the new set of records?And you are correct, CustId is not an Identity column.Thanks again for your help with this!SQL Newbie! |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-11 : 09:28:52
|
[code]update t1set t1.custid = (select min(t2.custid) from EIM_CUSTOMERS t2 where t2.custname = t1.custname)from EIM_CUSTOMERS t1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2006-12-11 : 09:33:39
|
T1 and T2 are called Aliases and they saves SQL Devs from having to type long and boring table names again and again. @t is a table variable and it is created just for this sample code, you can paste your original table name in it's place.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
BigKaTooMer
Starting Member
3 Posts |
Posted - 2006-12-11 : 10:01:58
|
Job Done. Thanks to you both for prompt and relevant support. It's comforting to know that expert knowledge is at hand!Thanks,WillSQL Newbie! |
 |
|
|
|
|
|
|