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

x
?
Solved

Translate query with alias table names from Access sql to T-sql

Posted on 2016-09-23
4
Medium Priority
?
81 Views
Last Modified: 2016-09-26
This one is driving me nuts... I know my way around Access pretty well and can manually translate simple queries to sql.  However, in this one I have to include the same table twice (AS dbo_Job and again as dbo_Job1) and join them to other tables.  I'm really struggling with this.  Would anyone care to guide me in the right direction.  Functional Access query is attached.  Thanks!

SELECT dbo_OrderCallOffSchedule.DatePlanned, dbo_Customer.CustomerCode, dbo_Job_1.JobReference, dbo_Customer.Name, dbo_Job_1.Address1, dbo_OrderHeader.OrderNumber, dbo_CustomerAddress.AddressCode, dbo_OrderLine.ManualDescription, dbo_CustomerAddress.Address1
FROM (((((dbo_OrderCallOffSchedule INNER JOIN dbo_OrderHeader ON dbo_OrderCallOffSchedule.OrderID = dbo_OrderHeader.OrderID) INNER JOIN dbo_OrderLine ON dbo_OrderCallOffSchedule.OrderLineID = dbo_OrderLine.OrderLineID) LEFT JOIN dbo_CustomerAddress ON dbo_OrderHeader.DeliveryAddressID = dbo_CustomerAddress.CustomerAddressID) LEFT JOIN dbo_Job ON dbo_CustomerAddress.CustomerAddressID = dbo_Job.CustomerAddressID) INNER JOIN dbo_Customer ON dbo_OrderHeader.CustomerID = dbo_Customer.CustomerID) LEFT JOIN dbo_Job AS dbo_Job_1 ON dbo_CustomerAddress.JobID = dbo_Job_1.JobID
WHERE (((dbo_OrderCallOffSchedule.DatePlanned)=#9/27/2016#))
ORDER BY dbo_Customer.Name, dbo_Job_1.Address1, dbo_CustomerAddress.AddressCode;

Open in new window

0
Comment
Question by:BobIa
[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
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
Daniel Van Der Werken earned 2000 total points
ID: 41812575
Try this. I tried to be careful, but I may have missed something.
SELECT 
		OCOS.DatePlanned, 
		CUST.CustomerCode, 
		JOB1.JobReference, 
		CUST.Name, 
		JOB1.Address1, 
		OH.OrderNumber, 
		CA.AddressCode, 
		OL.ManualDescription, 
		CA.Address1
FROM dbo_OrderCallOffSchedule OCOS
INNER JOIN dbo_OrderHeader OH
	ON OCOS.OrderID = OH.OrderID
INNER JOIN dbo_OrderLine OL
	ON OCOS.OrderLineID = OL.OrderLineID
LEFT JOIN dbo_CustomerAddress CA
	ON OH.DeliveryAddressID = CA.CustomerAddressID
LEFT JOIN dbo_Job JOB
	ON CA.CustomerAddressID = JOB.CustomerAddressID
INNER JOIN dbo_Customer CUST
	ON OH.CustomerID = CUST.CustomerID
LEFT JOIN dbo_Job JOB1 
	ON CA.JobID = JOB1.JobID
WHERE OCOS.DatePlanned = '9/27/2016'
ORDER BY CUST.Name, JOB1.Address1, CA.AddressCode;

Open in new window

0
 

Author Comment

by:BobIa
ID: 41812636
Daniel,
Thanks! You're a life saver... was down to last brain cell on this one!  I had to strip out and replace some underscores with periods but it works beautifully.  It also helps me to understand the relationships and alias procedure for sql.  Thanks again.  I am posting the final version here for those who come after looking for a solution like this.

SELECT 
		OCOS.DatePlanned, 
		CUST.CustomerCode, 
		JOB1.JobReference, 
		CUST.Name, 
		JOB1.Address1, 
		OH.OrderNumber, 
		CA.AddressCode, 
		OL.ManualDescription, 
		CA.Address1
FROM dbo.OrderCallOffSchedule OCOS
INNER JOIN dbo.OrderHeader OH
	ON OCOS.OrderID = OH.OrderID
INNER JOIN dbo.OrderLine OL
	ON OCOS.OrderLineID = OL.OrderLineID
LEFT JOIN dbo.CustomerAddress CA
	ON OH.DeliveryAddressID = CA.CustomerAddressID
LEFT JOIN dbo.Job JOB
	ON CA.CustomerAddressID = JOB.CustomerAddressID
INNER JOIN dbo.Customer CUST
	ON OH.CustomerID = CUST.CustomerID
LEFT JOIN dbo.Job JOB1 
	ON CA.JobID = JOB1.JobID
WHERE OCOS.DatePlanned = '9/27/2016'
ORDER BY CUST.Name, JOB1.Address1, CA.AddressCode

Open in new window

0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 41812640
Awesome. I provided you with my personal preferred way to format the SQL.

If all is well, please accept my solution as the answer.
0
 

Author Closing Comment

by:BobIa
ID: 41816575
Thanks Daniel
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

722 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