SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Two tables and want to join on different columns


Two tables and want to join on different columns

Author
Message
twdavis-893252
twdavis-893252
SSC-Enthusiastic
SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)SSC-Enthusiastic (119 reputation)

Group: General Forum Members
Points: 119 Visits: 363
I want to join two tables order_ship and ship_id. I want to join on location and order_no which both tables have the problem is my next field are order_ship.ship_num which is always one of two values 000001 or 100001 and then ship_id.ship_type which is always TypeA or TypeB.

order_ship.ship_num value of 000001 equals ship_id.ship_type value of TypeA
and
order_ship.ship_num value of 100001 equals ship_id.ship_type value of ship_id.TypeB

The table are the same other than these to fields.
RP_DBA
RP_DBA
SSC Eights!
SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)SSC Eights! (947 reputation)

Group: General Forum Members
Points: 947 Visits: 1070
Resolver table?

use tempdb
go

declare @order_ship table (
location char(5)
, order_no int
, ship_num char(6)
);

declare @ship_id table (
location char(5)
, order_no int
, ship_type char(5)
);

declare @resolver table (
ship_num char(6)
, ship_type char(5)
);

insert into @order_ship values ('US', 1, '000001'), ('US', 2, '000001'), ('US', 3, '100001'), ('US', 4, '100001');
insert into @ship_id values ('US', 1, 'TypeA'), ('US', 2, 'TypeA'), ('US', 3, 'TypeB'), ('US', 4, 'TypeB');

insert into @resolver values ('000001', 'TypeA'), ('100001', 'TypeB');

select *
from @order_ship o
inner join @resolver r on o.ship_num = r.ship_num
inner join @ship_id s
on o.location = s.location
and o.order_no = s.order_no
and s.ship_type = r.ship_type;



_____________________________________________________________________
- Nate

@nate_hughes
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search