473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Joining two values together across multiple tables

19
This is a bit hard to explain but I'll try my best.

I have 2 tables. One table has a row with the following value:

2,3

The two numbers are both ID's that are in the second table. I need to write a query that will ultimately give me the values of the second table, merged together as follows:

Data One, Data Two

Instead of just the id's. Is this possible? I tried to figure out how to use a Split function in SQL but then I'd run into the problem of how to get both pieces of data once the ID's are split...

MGM out
Apr 16 '08 #1
8 2433
siva538
44
You may probably consider using cursor for the first one and then retreive the data from second.
Apr 16 '08 #2
Similar to the cursor suggestion, I used a temp table to build the results then read them in. The final solution I had was to change the database design to use a join table to resolve the relationship into discreet records. (They can still come out as a comma del string for display purposes.) Once that was done, there were individual records for each value and are directly queriable without the mess in a sproc or other code.

Fortunately I *could* do that otherwise you're stuck with the mess.

_E
Apr 16 '08 #3
ck9663
2,878 Expert 2GB
This is a bit hard to explain but I'll try my best.

I have 2 tables. One table has a row with the following value:

2,3

The two numbers are both ID's that are in the second table. I need to write a query that will ultimately give me the values of the second table, merged together as follows:

Data One, Data Two

Instead of just the id's. Is this possible? I tried to figure out how to use a Split function in SQL but then I'd run into the problem of how to get both pieces of data once the ID's are split...

MGM out
Actually you can use your second table twice to relate the two keys. Just make sure to name the tables properly.

Paste this on your query analyzer:

Expand|Select|Wrap|Line Numbers
  1. set nocount on
  2.  
  3. declare @tbl1 table (col1 int, col2 int)
  4. declare @tbl2 table (colkey int, coldesc varchar(20))
  5.  
  6. insert into @tbl1 (col1, col2) values (1,2)
  7.  
  8. insert into @tbl2 (colkey, coldesc) values (1, 'Data One')
  9. insert into @tbl2 (colkey, coldesc) values (2, 'Data Two')
  10. insert into @tbl2 (colkey, coldesc) values (3, 'Data Three')
  11. insert into @tbl2 (colkey, coldesc) values (4, 'Data Four')
  12. insert into @tbl2 (colkey, coldesc) values (5, 'Data Five')
  13.  
  14. select * from @tbl1
  15.  
  16. select * from @tbl2
  17.  
  18. select tbl1.col1, key1.coldesc, tbl1.col2, key2.coldesc
  19. from @tbl1 tbl1 
  20.     inner join @tbl2 key1 on key1.colkey = tbl1.col1
  21.     inner join @tbl2 key2 on key2.colkey = tbl1.col2
  22.  
  23.  
Change the join from INNER to LEFT as necessary.

-- CK
Apr 16 '08 #4
siva538
44
Actually you can use your second table twice to relate the two keys. Just make sure to name the tables properly.

Paste this on your query analyzer:

Expand|Select|Wrap|Line Numbers
  1. set nocount on
  2.  
  3. declare @tbl1 table (col1 int, col2 int)
  4. declare @tbl2 table (colkey int, coldesc varchar(20))
  5.  
  6. insert into @tbl1 (col1, col2) values (1,2)
  7.  
  8. insert into @tbl2 (colkey, coldesc) values (1, 'Data One')
  9. insert into @tbl2 (colkey, coldesc) values (2, 'Data Two')
  10. insert into @tbl2 (colkey, coldesc) values (3, 'Data Three')
  11. insert into @tbl2 (colkey, coldesc) values (4, 'Data Four')
  12. insert into @tbl2 (colkey, coldesc) values (5, 'Data Five')
  13.  
  14. select * from @tbl1
  15.  
  16. select * from @tbl2
  17.  
  18. select tbl1.col1, key1.coldesc, tbl1.col2, key2.coldesc
  19. from @tbl1 tbl1 
  20.     inner join @tbl2 key1 on key1.colkey = tbl1.col1
  21.     inner join @tbl2 key2 on key2.colkey = tbl1.col2
  22.  
  23.  
Change the join from INNER to LEFT as necessary.

-- CK
This is not a scalable though ! If there are more columns, it won't work.
Apr 17 '08 #5
ck9663
2,878 Expert 2GB
Option 1:
1. Add more join, just make sure you alias each use of your table2.

Option 2:
1. Unpivot your table
2. Use a single join to get the data from table2. You will have an output that looks like:
Expand|Select|Wrap|Line Numbers
  1. COL, DESCRIPTION
  2. 1       Data One
  3. 2       Data Two
  4.  
3. Pivot your table back. To get the desired
Expand|Select|Wrap|Line Numbers
  1.  COL1, DESCRIPTION1, COL2, DESCRIPTION2
  2.    1       Data One             2       Data Two
No temp table necessary.

-- CK
Apr 17 '08 #6
siva538
44
Yes, second option is better one. But understand that there are code changes involved for more number of columns, where as if you go for a cursor your life becomes easier and using @@FETCH_STATUS you can identify n number of records and their corresponding values from table2.

HTH...
Apr 17 '08 #7
ck9663
2,878 Expert 2GB
Yes. There's always cursor.

I made the suggestion based on your example. Your sample data have two columns which I think JOIN would be sufficient enough. If you're talking about a lot of columns, it'll be your call.

I just use cursor as last resort.

-- CK
Apr 17 '08 #8
MGM
19
I actually ended up changing my table structure around so no need for all of the above. Thanks for the help however, it was a very interesting read to see what solutions you guys could come up with.

MGM out
Apr 18 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Job Lot | last post by:
Is there anyway of Joining two or more DataTable with similar structure? I have three DataTables with following structures Data, AmountB/F, Repayments, InterestCharged and AmountC/F i want...
6
by: Steve Jorgensen | last post by:
I tried to fix a problem for a client today in which report sections and even individual text controls in some of their reports are being split across page boundaries. Of course, I was thinking...
13
by: scorpion53061 | last post by:
Very urgent and I am very close but need a little help to get me over the edge........ I need to write these columns to a html file with each row containing these columns (seperated by breaks)....
2
by: Coquette | last post by:
Hi all. I'm new here. I wanted to ask your opinion about *joining two tables* from *two different database* together. Is *that* possible? My project requires me to join identical tables from...
3
by: mkjets | last post by:
I have worked for hours on trying to find a solution and have not figured it out. I am working in Access 2003. I need to create a query that takes values from 1 table and displays them in...
12
by: veaux | last post by:
Question about joins in queries. I have 2 tables with a field called "ID". Table 1 Rec1 = Jan12FredFlintstone Rec2 = Feb01WilmaRubble Table 2 Rec1 = Jan12BarneyRubble Rec2 = Mar03SamSlate
2
by: M | last post by:
I have about 14 tables that I am trying to tie together in a view to see a user's status in our company. Joe User: Email - Active ADP - Active Portal - Inactive etc. We need to track...
2
by: Supermansteel | last post by:
I am joining these 2 tables together in Access 2003 and can't figure out the exact way of writing this script......Can anyone help? I have the following SQL: SELECT...
4
by: knix | last post by:
I have this access project consisting of multiple tables that are linked together in a relationship. I would like to migrate the consolidated information through appending in a datasheet form or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.