SQL Syntax with Left Join with a Subquery?

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
BKennedy2008Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
it's redundant.  The join is essentially you're where.

But there's also no need for the subquery:

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

Open in new window

Mike EghtebasDatabase and Application DeveloperCommented:
select R.URICode
     , briefdescription
	 , BillCode, model
	 , ManName
	 , SERIALNUMBER
	 , HOMELOCATION
	 , G1.Scan_Date
	 , G1.Current_Location
	 , G1.JobNumber
	 , e.Scan_Date
	 , e.Filepath1 as JobNumber
from NewInventory R left outer join Scans e
      On e.upccode = R.URICode 
	  where R.URICode <='30126'
	  order by e.Scan_Date desc, R.URICode

Open in new window

BKennedy2008Author Commented:
Sorry, I might add in that The Scans table contains multiple scans that day for column URICOde, and I need the latest scan in the scan_Date  for that day to be the selected.
Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Mike EghtebasDatabase and Application DeveloperCommented:
;With CTE
As
(
Select Rank() Over(Partition by G.UPCCode order By e.Scan_Date) As rnk
	             , R.Current_Location
				 , e.Filepath1 as JobNumber
				 , R.URICode
 	             , e.Scan_Date
	             , e.JobNumber  
from NewInventory R left join Scans e
On e.upccode = R.URICode
where R.URICode <='30126')
Select R.Current_Location
	, e.Filepath1 as JobNumber
	, R.URICode
 	, e.Scan_Date
	, e.JobNumber
From CTE
where rnk = 1
Order by CTE.Scan_Date desc, CTE.URICode

Open in new window


I have include just a few fields. After this works, then you can add the remaining fields you want.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
awking00Information Technology SpecialistCommented:
Just need to modify eghtebas' solution to order by e.Scan_Date DESC
BKennedy2008Author Commented:
Thanks- I did a little mod from egtebas and it works. Thanlks Again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.