SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Left outer join with TOP 1 Select
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rtown
Yak Posting Veteran

53 Posts

Posted - 11/04/2013 :  19:56:42  Show Profile  Reply with Quote
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

India
165 Posts

Posted - 11/04/2013 :  22:56:20  Show Profile  Reply with Quote
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

Edited by - VeeranjaneyuluAnnapureddy on 11/04/2013 22:57:49
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 11/05/2013 :  00:40:55  Show Profile  Reply with Quote
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 - 11/05/2013 :  12:44:58  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 11/05/2013 :  12:56:01  Show Profile  Reply with Quote
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 - 11/05/2013 :  13:19:05  Show Profile  Reply with Quote
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 - 11/05/2013 :  14:00:22  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000