Solved

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

Posted on 2009-04-03
6
294 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access sql to sql server express 10 32
MS SQL Merging data from table into another table 1 32
SSRS 2013 - Overlapping reports 2 19
Help Required 3 92
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

785 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