Avatar of BKennedy2008
BKennedy2008
 asked on

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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
BKennedy2008

8/22/2022 - Mon
Kyle Abrahams

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 Eghtebas

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

BKennedy2008

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Mike Eghtebas

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
awking00

Just need to modify eghtebas' solution to order by e.Scan_Date DESC
BKennedy2008

ASKER
Thanks- I did a little mod from egtebas and it works. Thanlks Again!