Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Left outer join with TOP 1 Select

Author  Topic 

rtown
Yak Posting Veteran

53 Posts

Posted - 2013-11-04 : 19:56:42
Hello all,
This command is becoming a behemoth. I just need to add one more thing, and I am unsure how to do it because of the TOP 1 requirement.

I want to add this select:

SELECT TOP 1 ShopComments.cpid, ShopComments.drawingid, ShopComments.shopcomment WHERE ShopComments.drawingid = Drawings.ID ORDER BY ShopComments.cpid ASC


To this command, as a left outer join. Meaning there may be no records but I still want the primary data.

SELECT
Drawings.ID, Drawings.Job, Drawings.Series, Drawings.Returned, Drawings.Description, Drawings.Revision, Drawings.Status, Drawings.ShopRevision, Drawings.DrawingFilename, Drawings.DwgDue, Drawings.DSaw, Drawings.DRobotics, Drawings.DOrdered, Drawings.DShopStatus, Drawings.DEstHours, Drawings.DRush, Drawings.ShopComments,
Jobs.ID AS jobid, Jobs.Quote, Jobs.Job, Jobs.Customer, Jobs.JobName, Jobs.Active, Jobs.NDT, Jobs.Inspection,
Python.PJob, Python.PSeries, Python.Completed
FROM Drawings
INNER JOIN Jobs ON Drawings.Job = Jobs.Job
LEFT OUTER JOIN Python ON Python.PJob = Drawings.Job AND Python.PSeries = Drawings.Series
WHERE Drawings.Status='Shop Issued' AND Jobs.Active='True' AND Drawings.DOrdered='Yes' AND DShopStatus IS NULL
OR Drawings.Status='Shop Issued' AND Jobs.Active='True' AND Drawings.DOrdered='Yes' AND DShopStatus='Awaiting Production
ORDER BY CONVERT(Date, DwgDue, 103) ASC


Wow, I hope that makes sense. Any advice appreciated. I looked all over for some info, and found some on OUTER APPLY but this didn't yield any results...
Thanks!

VeeranjaneyuluAnnapureddy
Posting Yak Master

169 Posts

Posted - 2013-11-04 : 22:56:20
SELECT Drawings.ID
, Drawings.Job
, Drawings.Series
, Drawings.Returned
, Drawings.Description
, Drawings.Revision
, Drawings.Status
, Drawings.ShopRevision
, Drawings.DrawingFilename
, Drawings.DwgDue
, Drawings.DSaw
, Drawings.DRobotics
, Drawings.DOrdered
, Drawings.DShopStatus
, Drawings.DEstHours
, Drawings.DRush
, Drawings.ShopComments
, Jobs.ID AS jobid
, Jobs.Quote
, Jobs.Job
, Jobs.Customer
, Jobs.JobName
, Jobs.Active
, Jobs.NDT
, Jobs.Inspection
, Python.PJob
, Python.PSeries
, Python.Completed
FROM Drawings
INNER JOIN Jobs ON Drawings.Job = Jobs.Job
LEFT OUTER JOIN Python ON Python.PJob = Drawings.Job AND Python.PSeries = Drawings.Series
WHERE Drawings.Status='Shop Issued' AND Jobs.Active='True' AND Drawings.DOrdered='Yes' AND DShopStatus IS NULL
OR DShopStatus='Awaiting Production'
ORDER BY CONVERT(Date, DwgDue, 103) ASC


veeranjaneyulu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-05 : 00:40:55
as per your explanation you need OUTER APPLY only


SELECT
Drawings.ID, Drawings.Job, Drawings.Series, Drawings.Returned, Drawings.Description, Drawings.Revision, Drawings.Status, Drawings.ShopRevision, Drawings.DrawingFilename, Drawings.DwgDue, Drawings.DSaw, Drawings.DRobotics, Drawings.DOrdered, Drawings.DShopStatus, Drawings.DEstHours, Drawings.DRush, Drawings.ShopComments,
Jobs.ID AS jobid, Jobs.Quote, Jobs.Job, Jobs.Customer, Jobs.JobName, Jobs.Active, Jobs.NDT, Jobs.Inspection,
Python.PJob, Python.PSeries, Python.Completed
FROM Drawings
INNER JOIN Jobs ON Drawings.Job = Jobs.Job
LEFT OUTER JOIN Python ON Python.PJob = Drawings.Job AND Python.PSeries = Drawings.Series
OUTER APPLY (SELECT TOP 1 ShopComments.cpid, ShopComments.drawingid, ShopComments.shopcomment
FROM ShopComments
WHERE ShopComments.drawingid = Drawings.ID ORDER BY ShopComments.cpid ASC
)sc

WHERE Drawings.Status='Shop Issued' AND Jobs.Active='True' AND Drawings.DOrdered='Yes' AND DShopStatus IS NULL
OR Drawings.Status='Shop Issued' AND Jobs.Active='True' AND Drawings.DOrdered='Yes' AND DShopStatus='Awaiting Production
ORDER BY CONVERT(Date, DwgDue, 103) ASC


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

rtown
Yak Posting Veteran

53 Posts

Posted - 2013-11-05 : 12:44:58
quote:
Originally posted by visakh16

as per your explanation you need OUTER APPLY only


SELECT
Drawings.ID, Drawings.Job, Drawings.Series, Drawings.Returned, Drawings.Description, Drawings.Revision, Drawings.Status, Drawings.ShopRevision, Drawings.DrawingFilename, Drawings.DwgDue, Drawings.DSaw, Drawings.DRobotics, Drawings.DOrdered, Drawings.DShopStatus, Drawings.DEstHours, Drawings.DRush, Drawings.ShopComments,
Jobs.ID AS jobid, Jobs.Quote, Jobs.Job, Jobs.Customer, Jobs.JobName, Jobs.Active, Jobs.NDT, Jobs.Inspection,
Python.PJob, Python.PSeries, Python.Completed
FROM Drawings
INNER JOIN Jobs ON Drawings.Job = Jobs.Job
LEFT OUTER JOIN Python ON Python.PJob = Drawings.Job AND Python.PSeries = Drawings.Series
OUTER APPLY (SELECT TOP 1 ShopComments.cpid, ShopComments.drawingid, ShopComments.shopcomment
FROM ShopComments
WHERE ShopComments.drawingid = Drawings.ID ORDER BY ShopComments.cpid ASC
)sc

WHERE Drawings.Status='Shop Issued' AND Jobs.Active='True' AND Drawings.DOrdered='Yes' AND DShopStatus IS NULL
OR Drawings.Status='Shop Issued' AND Jobs.Active='True' AND Drawings.DOrdered='Yes' AND DShopStatus='Awaiting Production
ORDER BY CONVERT(Date, DwgDue, 103) ASC


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




Thank you visakh16, unfortunately I had tried an outer apply as well and didn't yield any results when there are in fact records that should be displayed. No errors, so I assume something is not functioning as intended.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-05 : 12:56:01
May be the rule you specify are not correct. Are you sure there are records satisfying condition ShopComments.drawingid = Drawings.ID in table ShopComments?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

rtown
Yak Posting Veteran

53 Posts

Posted - 2013-11-05 : 13:19:05
quote:
Originally posted by visakh16

May be the rule you specify are not correct. Are you sure there are records satisfying condition ShopComments.drawingid = Drawings.ID in table ShopComments?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs



Yes I am sure, I tested the select alone within MS SQL Manager and it did indeed pull the top 1 record. I am not very familiar with outer apply, in fact this is the first time I have used this. Perhaps I am missing something.
Go to Top of Page

rtown
Yak Posting Veteran

53 Posts

Posted - 2013-11-05 : 14:00:22
Just a thought but perhaps it's the method that I am trying to access the results of the outer apply. If I use SC to access them it gives me no results. With a recordset SC I get an error as the program does not recognize this variable, likewise if I try and access the field name ie: recordset shopcomment. So to summarize, maybe I am not understanding how to access the record that may in fact exist...
Go to Top of Page
   

- Advertisement -