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 |
|
skillile
Posting Yak Master
208 Posts |
Posted - 2002-02-21 : 16:06:36
|
| I have a table with a list of offices that I am trying to assigna corporate HQ for the data might look like thisofficeid officename12345 ABC Company9877 ABC Company7878 ABC Co98387 XYZ Company8973 XYZ CoI know that I have problems with the officenames being inputeddifferently and that is going to be tough to pull like names but,my question is. I want to parse the table and find all the like offices andassign them a unique corporate id ie.corpid officeid officename 1 12345 ABC Company 1 9877 ABC Company 1 7878 ABC Co 2 98387 XYZ Company 2 8973 XYZ CoAny suggestions on how to get the corpid assigned.Thanksslow down to move faster... |
|
|
chadmat
The Chadinator
1974 Posts |
Posted - 2002-02-21 : 16:20:47
|
| I would work first on standardizing OfficeName. Once that is done, it will make adding the corporate ID much easier.-Chad |
 |
|
|
skillile
Posting Yak Master
208 Posts |
Posted - 2002-02-21 : 16:26:51
|
| Yep, thats in the mix but lets assume the officenamesmatch ie.xyzxyzxyznext step would beslow down to move faster... |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-02-21 : 16:43:28
|
quote: corpid officeid officename1 12345 ABC Company1 9877 ABC Company1 7878 ABC Co2 98387 XYZ Company2 8973 XYZ Co
-- create the new table with idscreate table1 (corpid int, officeid int, officename varchar(50))-- insert the old data while assigning a dummy 0 value to corpidinsert table1select corpid=0, officeid, officename from originalTabledeclare @i int, @officename varchar(50)select @i=1-- loop through rows with dummy valueswhile exists (select * from table1 where corpid=0)begin-- grab next blank officenameselect @officename=officename from table1 where corpid=0-- assign corpidupdate table1set corpid=@iwhere officename = @officename-- iterateselect @i=@i+1end |
 |
|
|
jbkayne
Posting Yak Master
100 Posts |
Posted - 2002-02-21 : 16:49:15
|
| Or do it without a cursor...update Company set [Id] = Result.[ID]from Company inner join (select OfficeName , (select Count(*) from ( select distinct OfficeName from Company ) as Company where Company.OfficeName <= Cmp.OfficeName) as [ID] from ( select distinct OfficeName from Company ) as Cmp ) as Result on Result.OfficeName = Company.OfficeName |
 |
|
|
Lou
Yak Posting Veteran
59 Posts |
Posted - 2002-02-22 : 00:20:01
|
It's not technically a cursor. It's slightly faster than a cursor. But it's simple for my simple mind. |
 |
|
|
|
|
|