Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2016-09-23
4
Medium Priority
?
86 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
  • 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

886 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