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
Solved

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

Posted on 2016-09-23
4
56 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 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

856 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