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 |
elhallouf
Starting Member
1 Post |
Posted - 2008-05-20 : 08:41:24
|
I have a table (cust_name, cust_number, parent_number) like :Name1--11111--22222Name2--22222--55555Name2--22222--55555Name3--33333--33333Name4--44444--11111Name5--55555--44444..I want to add a column parent_name as :Name1--11111--22222--Name2Name2--22222--55555--Name5Name2--22222--55555--Name5Name3--33333--33333--Name3Name4--44444--11111--Name1Name5--55555--44444--Name4As you can see, I try to get the corresponding name for the parent client accordingly to the relationshio cust_name, cust_numberNote that I can have the same cust_number repated in several rows, BUT the couple (cust_number, cust_name) is UNIQUE .. so the correspondance should be UNIQUEThis should be done by an AUTO JOIN on sql, but I am getting hard time to write the sql code since I couldn't get the same number or records..Thanks |
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-05-20 : 08:49:40
|
[code]declare @t table(cust_name varchar(20), cust_number int, parent_number int,Parent_Name varchar(20))insert @tselect 'Name1',11111,22222, null union allselect 'Name2',22222,55555, null union allselect 'Name2',22222,55555, null union allselect 'Name3',33333,33333, null union allselect 'Name4',44444,11111, null union allselect 'Name5',55555,44444, nullupdate t1set Parent_Name = t2.Cust_Namefrom @t t1 join @t t2 on t2.cust_number = t1.parent_numberselect * from @t[/code]Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
|
|
|
|