2008 sql server join query
I have the following tables:
-
Client information
CustCode, Name , Address, CityID
-
MasterCity
CityName, CityId
-
Order_Details
OrderDetails, CustCode , OrderNo, Somedetails , DeliveryStation
Deliverystation
is where the order should be placed. Here I post CityId
and customer information also contains CityId
and not cityname
.
Now I want to select data from OrderDetails
. I join OrderDetails
and Customer Details
On Customer Details.CustCode = OrderDetails.CustCod
e and Customer Details
and MasterCity on MasterCity.CityId = Customer Details.CityId
.
this job is great, but I also want to join OrderDetails
and MasterCity
to get the Deliverystation
City Name.
How can i do this?
source to share
Just add another connection to MasterCity with an alias like
Select a.CustCode, a.Name, a.Address, A.CityId,c.OrderNo, c.SomeDetails,d.CityName DeliveryStation
from CustomerDetails a
join MasterCity b
on a.CityID=b.CityID
join OrderDetail c
on a.CustCode=c.CustCode
join MasterCity d
on d.CityId=c.DeliveryStation
source to share