The query below will pull what I need but pulls all obsvalues and I only want it to pull the most recent. The completeddate is probably what needs to be used in some sort of nested select but I'm not sure how to do this and return values for all rather than just values for one person.
SELECT distinct obsvalue, p.person_id, p.last_name, p.first_name, p.person_nbr, o2.completeddate
FROM order_ o2
INNER JOIN ncsuds_lab_test_desc d ON RTRIM(o2.actText) = RTRIM(d.test_desc) and d.order_ind = 'Y' AND d.selected_diag_id = 42
inner join person p on o2.person_id = p.person_id
WHERE o2.obsvalue IS NOT NULL
AND ISNUMERIC(o2.obsvalue) = 1
and o2.completeddate > '20150101'
order by completeddate desc
Which column defines 'most recent'? If you mean the most recent o2.completedate, then you'll need a MAX.
Open in new window
btw note the use of indentation and capitalizing all SELECT .. FROM ... keywords. Makes it easier to read. Also I removed the DISTINCT as the GROUP BY handles that.Check out SQL Server GROUP BY Solutions for some extra info on grouping.