Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-03
6
Medium Priority
?
302 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

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 70

Accepted Solution

by:
Scott Pletcher earned 2000 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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

688 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