We help IT Professionals succeed at work.
Get Started

SQL Syntax with Left Join with a Subquery?

220 Views
Last Modified: 2015-06-09
Can this be done (Left Join on a sub select query with a where? The Bold is stating that Multi-part identifier cannot be bound..Not sure how to re-write this to get the same results, other than below in my "Non-efficient way"

select R.URICode, briefdescription,
BillCode, model, ManName, SERIALNUMBER,HOMELOCATION, G1.Scan_Date,G1.Current_Location, G1.JobNumber
from NewInventory R
left outer join
(Select top 1 Scan_Date, Current_Location, Filepath1 as JobNumber, UPCCode from Scans e where e.upccode = R.URICode order by Scan_Date desc ) G1 on R.URICODE = G1.UPCCode
where R.URICode <='30126'
order by R.URICode asc


Non-Efficient Way:
select  b.URICode, briefdescription, BillCode, model, ManName, SERIALNUMBER,HOMELOCATION,
(select top 1 Scan_Date from Scans e where upccode =  b.uricode order by Scan_Date desc)as Last_Scan_Date,
 (select top 1 Current_Location from Scans e where upccode =  b.uricode order by Scan_Date desc)as Current_Location,
 (select top 1 FilePath1 as JobNumber from Scans e where upccode =  b.uricode order by Scan_Date desc)as JobNumber
 
from NewInventory  b
left join urijobs.dbo.jobs on (select top 1 FilePath1 as JobNumber from Scans e where upccode =  b.uricode order by Scan_Date desc) = urijobs.dbo.jobs.Job_Number
left join urijobs.dbo.Job_Status on urijobs.dbo.Job_Status.Status_ID = urijobs.dbo.jobs.[status]
where b.URICode <='30126'
order by b.URICode asc
Comment
Watch Question
Database and Application Developer
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE