Solved

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

Posted on 2009-04-03
6
293 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

919 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

16 Experts available now in Live!

Get 1:1 Help Now