Solved

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

Posted on 2009-04-03
6
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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:
Scott Pletcher 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

738 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