Start Free Trial
Come for the solution, stay for everything else.
Start Free Trial
SQL Select from 2 tables
I have a sql query question.
I have 2 tables... Projects and ProjComments
Projects has 2 fields ProjID and ProjName
ProjComments has 4 fields. ProjCommentID, ProjID, CommentDate and Comment
There can be several comments on one project
what sql code would I use to select all projects and only the most recent
comment for each project? Some projects won't have any comments.
so my results would include
ProjID, ProjName, comment Date and Comment.
Microsoft SQL Server 2005
Microsoft SQL Server
Microsoft SQL Server 2008
8/22/2022 - Mon
Guy Hengel [angelIII / a3]
you can check this article:
ASKER CERTIFIED SOLUTION
Log in or sign up to see answer
Become an EE member today
7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
p.ProjID, p.ProjName, pc.CommentDate, pc.Comment
FROM Projects p
LEFT OUTER JOIN (
SELECT ProjID, CommentDate, Comment,
ROW_NUMBER() OVER(PARTITION BY ProjID ORDER BY CommentDate DESC) AS row_num
) AS pc ON
pc.row_num = 1 AND
pc.ProjID = p.ProjID
--ORDER BY p.ProjID | ORDER BY p.ProjName
Thank you this worked well
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!
Plans and Pricing
Certified Expert Program
© 1996-2022 Experts Exchange, LLC. All rights reserved. Covered by US Patent