I am working on a report query where I need only the highest most recent row using a date range, as well as patientid, and Type_Name for filters . The int column 'ednum' can have many rows on any particular date, i just need the row with the highest ednum for that patient in the date range.
I am using a single table attempting to drill down to show one ednum value per date (ednum value being MAX) Here is my attempt, it runs but is not giving the MAX(ednum) value but including additional rows of the same date.
MS SQL 2008
Code:SELECT TP2.ednum, TP2.BackgroundID, TP2.Patient_No, TP2.Last_Name, TP2.Visit_Name, TP2.SessionDT FROM TypePatient AS TP1 INNER JOIN (SELECT ednum, BackgroundID, CONVERT(varchar, DATE_, 101) AS SessionDT, Patient_No, Last_Name, Visit_Name FROM TypePatient WHERE (Visit_Name = 'Progress Note')) AS TP2 ON TP1.BackgroundID = TP2.BackgroundID AND TP1.ednum = (SELECT MAX(ednum) AS ednum FROM TypePatient WHERE (BackgroundID = 3304) AND (TP2.SessionDT BETWEEN '09/20/2015' AND '09/26/2015') AND (Visit_Name = 'Progress Note') ORDER BY TP2.SessionDT) GROUP BY TP2.SessionDT, TP2.ednum, TP2.BackgroundID, TP2.Patient_No, TP2.Last_Name, TP2.Visit_Name, TP2.ednum




Reply With Quote
