How can I select the most recent date entry?

Hi, I'm using sql 2005 express
I would like to select one entry from dbo.DailyStepsGoals based on the studyid  and ds.DateGoalIsSet (DateTime) is the most recent.  How can I put that in Tsql?  Thank you.
select p.StudyId, d.LastName, p.DateOfFirstVisit, ds.TargetNumber as StepTargetNumber, 
from dbo.Patients p inner join dbo.Doctors d on p.DoctorId = d.DoctorId
left join dbo.DailyStepsGoals ds on p.StudyId = ds.StudyId and ds.DateGoalIsSet

Open in new window

lapuccaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JoeNuvoCommented:
Is it this one?

SELECT TOP 1 p.StudyId, d.LastName, p.DateOfFirstVisit, ds.TargetNumber as StepTargetNumber
from dbo.Patients p 
inner join dbo.Doctors d on p.DoctorId = d.DoctorId
left join dbo.DailyStepsGoals ds on p.StudyId = ds.StudyId
ORDER BY p.StudyId DESC, ds.DateGoalIsSet DESC

Open in new window

0
Pratima PharandeCommented:
Select P.StudyId,d.LastName, p.DateOfFirstVisit, ds.TargetNumber as StepTargetNumber
From dbo.Patients p inner join dbo.Doctors d on p.DoctorId = d.DoctorId
left join dbo.DailyStepsGoals ds on p.StudyId = ds.StudyId
where ds.DateGoalIsSet = (select Max(d2.DateGoalIsSet)
from DailyStepsGoals d2 where d2.StudyId= p.StudyId)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lapuccaAuthor Commented:
Joe, sorry, I have a couple of this type selection in this query fro the dg table I also need the most recent DateGoalSet column.  I don't think select Top 1 will select the most recent from both dg and ds table?  Patient to both ds and dg is one to many relationship.,  Thank you.

select p.StudyId, d.LastName, p.DateOfFirstVisit, ds.TargetNumber as StepTargetNumber,
from dbo.Patients p inner join dbo.Doctors d on p.DoctorId = d.DoctorId
left join dbo.DailyStepsGoals ds on p.StudyId = ds.StudyId  and ds.DateGoalIsSet
left join dbo.DietGoals dg on p.StudyId = dg.StudId and dg.DateGoalIsSet
0
lapuccaAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.