December 23, 2019 at 7:25 pm
December 23, 2019 at 7:33 pm
You'll learn a lot more if you do your own homework.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
December 23, 2019 at 7:34 pm
it would be nice to at least see what you've tried so far.
For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
December 23, 2019 at 9:59 pm
SELECT p.ID PRODUCT_ID,
p.PRODUCT_NAME,
o.ID ORD_ID,
o.DATE_ORDERED
FROM dbo.S_PRODUCT p
OUTER APPLY(SELECT TOP(2)
o.ID
o.DATE_ORDERED
FROM dbo.S_ITEM i
INNER JOIN dbo.S_ORD o
ON o.ID = i.ORD_ID
WHERE i.PRODUCT_ID = p.ID
ORDER BY o.DATE_ORDERED DESC) o
December 23, 2019 at 11:18 pm
SELECT p.ID PRODUCT_ID,
p.PRODUCT_NAME,
o.ID ORD_ID,
o.DATE_ORDERED
FROM dbo.S_PRODUCT p
OUTER APPLY(SELECT TOP(2)
o.ID
o.DATE_ORDERED
FROM dbo.S_ITEM i
INNER JOIN dbo.S_ORD o
ON o.ID = i.ORD_ID
WHERE i.PRODUCT_ID = p.ID
ORDER BY o.DATE_ORDERED DESC) o
Although I believe in Christmas spirit, I don't think giving them the answer is really going to help them in the long run. :/
Thom~
Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
Larnu.uk
December 24, 2019 at 12:16 am
Although I believe in Christmas spirit, I don't think giving them the answer is really going to help them in the long run. :/
I don't think you can come up with that answer very easily if you are a beginner. It's sometimes best to see an actual answer, then see out how it works, than just being stuck on a problem getting nowhere.
December 24, 2019 at 12:47 am
Thom A wrote:Although I believe in Christmas spirit, I don't think giving them the answer is really going to help them in the long run. :/
I don't think you can come up with that answer very easily if you are a beginner. It's sometimes best to see an actual answer, then seeing out how it works, than just being stuck on a problem getting nowhere.
Nice formatting, as well, Jonathan!
--Jeff Moden
Change is inevitable... Change for the better is not.
December 24, 2019 at 9:55 pm
Thanks for feedback responses everyone and yes its better to work backwards from answer and possibly explain your code syntax logic. I just learning sql. Did not know what sql was until 2019.
December 25, 2019 at 4:49 am
Thanks for feedback responses everyone and yes its better to work backwards from answer and possibly explain your code syntax logic. I just learning sql. Did not know what sql was until 2019.
With that thought in mind, have you figured out what the Outer Apply in the code is doing for you and why Jonathan didn't use CROSS APPLY, instead?
--Jeff Moden
Change is inevitable... Change for the better is not.
January 3, 2020 at 3:39 pm
Thanks for feedback responses everyone and yes its better to work backwards from answer and possibly explain your code syntax logic. I just learning sql. Did not know what sql was until 2019.
See also these diagrams, which you can use to find your own answers or to better understand any answers others give you.
https://stevestedman.com/2015/05/tsql-join-types-poster-version-4-1/
-- webrunner
-------------------
A SQL query walks into a bar and sees two tables. He walks up to them and asks, "Can I join you?"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply