Solved

combining two tables, codes table references multiple records in the second table

Posted on 2009-04-03
6
292 Views
Last Modified: 2013-11-05
Table1 has a column called Codes with values such as:

USA-AUS-GBR-FRA

Table2 has columns which expand these codes, such as:
Code column: USA
Country column: United States
Type column: Country

I need to somehow combine these tables in a way that I can reference the full, expanded names from Table2, as well as the Type column, so they can be added to the Where clause. However, I'm completely lost as to how to do this, so I would really appreciate any help. Thank you!
0
Comment
Question by:bbdesign
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 24059137
USA-AUS-GBR-FRA

Is this a single value (one row), or multiple values in the same column (multiple rows) ?
0
 

Author Comment

by:bbdesign
ID: 24059152
Its a single value. The database was built with a single column for Codes, and they added codes separated by dashes like that.
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24059154
select table1.codes,table2.countrycolumn,table2.typecolumn from table1
join table2 on table1.codes=table2.codes
where table1.codes='USA'
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

Author Comment

by:bbdesign
ID: 24060492
The two columns don't match up exactly, the one column has the values combined with dashes, so a direct join won't work I would guess.
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 24063213
You can always use the "brute force" method ... I can't think of a cleaner method right now.


SELECT table1.col1, table1.col2, --...
    table21.country + ISNULL(',' + table22.country, '') + ISNULL(',' + table23.country, '') +
    ISNULL(',' + table24.country, '') + ISNULL(',' + table25.country, ''),  -- + ...
    ...
FROM table1
--I assumed one code was REQUIRED; if not, change this to also use LEFT OUTER JOIN method
INNER JOIN table2 t21 ON t21.code = SUBSTRING(t1.codes, 01, 3)
LEFT OUTER JOIN table2 t22 ON LEN(t1.codes) > 03 AND t22.code = SUBSTRING(t1.codes, 05, 3)
LEFT OUTER JOIN table2 t23 ON LEN(t1.codes) > 07 AND t23.code = SUBSTRING(t1.codes, 09, 3)
LEFT OUTER JOIN table2 t24 ON LEN(t1.codes) > 11 AND t24.code = SUBSTRING(t1.codes, 13, 3)
LEFT OUTER JOIN table2 t25 ON LEN(t1.codes) > 15 AND t25.code = SUBSTRING(t1.codes, 17, 3)
--...
0
 

Author Comment

by:bbdesign
ID: 24115312
I ended up writing a script to modify the database instead, doing it on the fly with a query was proving too problematic. People setup DBs like this thinking something is going to be easy, and it just isn't. Thanks for the solution, though.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how the fundamental information of how to create a table.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now