April 16, 2012 at 9:19 am
Is it possible to JOIN two tables by combining two columns in one table into a "key" and join it to the other tables "key".
Something like this:
begin tran
CREATE TABLE #users(id INT PRIMARY KEY, selection INT, period INT)
INSERT #users VALUES (1, 1, 1)
INSERT #users VALUES (2, 1, 2)
INSERT #users VALUES (3, 1, 3)
INSERT #users VALUES (4, 1, 4)
CREATE TABLE #user_files(id INT PRIMARY KEY, userid INT, number INT)
INSERT #user_files VALUES (1, 1, 11)
INSERT #user_files VALUES (2, 2, 12)
INSERT #user_files VALUES (3, 3, 13)
INSERT #user_files VALUES (4, 4, 14)
select cast(selection as varchar(1))+cast(period as varchar(1)) as number from #users
rollback
I would like to join #users.number=user_files.number and #users.id=#user_files.userid aswell, is that possible?
Ty for your time
April 16, 2012 at 9:25 am
I don't see a column #users.number in your definition shown above.
April 16, 2012 at 9:30 am
April 16, 2012 at 9:40 am
Something like this:
CREATE TABLE #users(id INT PRIMARY KEY, selection INT, period INT)
INSERT #users VALUES (1, 1, 1)
INSERT #users VALUES (2, 1, 2)
INSERT #users VALUES (3, 1, 3)
INSERT #users VALUES (4, 1, 4)
CREATE TABLE #user_files(id INT PRIMARY KEY, userid INT, number INT)
INSERT #user_files VALUES (1, 1, 11)
INSERT #user_files VALUES (2, 2, 12)
INSERT #user_files VALUES (3, 3, 13)
INSERT #user_files VALUES (4, 4, 14)
SELECT
*
FROM
[#users] u
INNER JOIN [#user_files] uf
ON (u.[id] = uf.[userid]
AND CAST(CAST(u.selection AS varchar) + CAST(u.period AS varchar) AS INT) = uf.[number])
;
DROP TABLE [#users];
DROP TABLE [#user_files];
April 16, 2012 at 9:45 am
Interesting, think it might work, ill give it a shot!
Thanks! 🙂
April 16, 2012 at 9:50 am
If the range of values for u.selection and u.period are constrained to single digit integers [1-9], why not skip the cast as varchar/int parts and just work with integer values, like this...
SELECT
*
FROM
[#users] u
INNER JOIN [#user_files] uf
ON (u.[id] = uf.[userid]
AND ((u.selection * 10) + u.period) = uf.[number])
;
If the range of values is NOT constrained to single digit integers, I don't know how either method would work reliably.
Rob Schripsema
Propack, Inc.
April 16, 2012 at 9:54 am
Rob Schripsema (4/16/2012)
If the range of values for u.selection and u.period are constrained to single digit integers [1-9], why not skip the cast as varchar/int parts and just work with integer values, like this...
SELECT
*
FROM
[#users] u
INNER JOIN [#user_files] uf
ON (u.[id] = uf.[userid]
AND ((u.selection * 10) + u.period) = uf.[number])
;
If the range of values is NOT constrained to single digit integers, I don't know how either method would work reliably.
Hard to say based solely on the sample data. For example, the actual data could have 14, 12 in the user table and 1412 in the user_files table. Simply multiplying by 10 wouldn't work, would it?
April 16, 2012 at 9:55 am
Great suggestion, luckly they are constrained to 1-4.
April 16, 2012 at 9:57 am
Hmm is it an absolute must to use the virtual tables u and uf?
April 16, 2012 at 9:59 am
Lynn,
You're absolutely correct. There are quite a few unknowns here.
But I made qualified assumption based on the OP's initial post, which said,
select cast(selection as varchar(1))+cast(period as varchar(1)) as number from #users
Rob Schripsema
Propack, Inc.
April 16, 2012 at 10:01 am
memymasta (4/16/2012)
Hmm is it an absolute must to use the virtual tables u and uf?
Those are table alaises, and I highly recomment using them. In fact, I should have specified each column in the select list using the table alais.column name instead of using the *.
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply