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
 General SQL Server Forums
 New to SQL Server Programming
 mapping between two tables

Author  Topic 

goligol
Posting Yak Master

128 Posts

Posted - 2011-12-05 : 09:21:14
I would like to write queries for the following problem:

I have the following tables:

table one:
hs_code ust_code district port share
848071 1220 01 06 0.0130392831238073
848071 1220 01 15 0.975529250994227
848071 1220 01 18 0.0114314658819652
848071 1220 02 07 0.00273487477707323
848071 1220 02 09 0.00779612804098788

I want to map each 6 digit hs_code to 10 digit hs_code in the following table for each port in the above table

table two:
hs_code ust_code district
8480718045 1220 01
8480718060 1220 01
8480718045 1220 02
8480718060 1220 02

for example "848071 1220 01" in table two is related to 3 ports 06,15, and 18 in table one. all 10 digits which share the same 6 digits with ust_code 1220 in distinct 01 should map to these 3 ports with the share affiliated to them)
where I can have the final product as:
hs_code ust_code district port share
8480718045 1220 01 06 0.0130392831238073
8480718045 1220 01 15 0.975529250994227
8480718045 1220 01 18 0.0114314658819652
8480718060 1220 01 06 0.0130392831238073
8480718060 1220 01 15 0.975529250994227
8480718060 1220 01 18 0.0114314658819652
8480718045 1220 02 07 0.00273487477707323
8480718045 1220 02 09 0.00779612804098788
8480718060 1220 02 07 0.00273487477707323
8480718060 1220 02 09 0.00779612804098788

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-12-05 : 09:51:21
select ...
from table1 t1
join table2 t2
on left(t2.hs_code,6 = t1.hs_code
and t2.ust_code = t1.ust_code



==========================================
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

goligol
Posting Yak Master

128 Posts

Posted - 2011-12-05 : 11:41:50
Thank you
Go to Top of Page
   

- Advertisement -