Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
table below shows a view of patient and their heightweight table . from this table i want to select height, weight, PatientId with max date in each moth for each patient ,Patient must have same birth date

PatientIDHeightMeasuredDateBirthDate....Weight
115012/12/2013 3/2/198760
11502/12/20133/2/198764
115024/12/20133/2/198759
216012/12/20133/2/198765
216019/12/20133/2/198763
216026/12/20133/2/198760
21605/12/20133/2/198766
11505/11/20133/2/198767
115012/11/20133/2/198766.5
115019/11/20133/2/198766
115026/11/20133/2/198767
215012/11/20133/2/198767
21504/11/20133/2/198768
215019/11/20133/2/198767
215029/11/20133/2/198768
316012/12/20133/2/198765
316019/12/20133/2/198763
316026/12/20133/2/198760
31605/12/20133/2/198766
416026/12/20133/2/198960
41605/12/20133/2/198966

ie result is

PatientIDHeightMeasuredDateBirthDate....Weight
115024/12/20133/2/198759
216026/12/20133/2/198760
115026/11/20133/2/198767
215029/11/20133/2/198768
316026/12/20133/2/198760

patient 4 is omitted since birth date is different and maximum date for each month is taken for each patient and there is value each month
PLZ help
Posted
Updated 19-Dec-13 20:51pm
v3
Comments
Maciej Los 20-Dec-13 2:29am    
"maximum date for each month is taken" - it's not true. Patients (1 and 2) are taken two times.
rajin kp 20-Dec-13 4:05am    
sorry for my bad English . what i mean that in each month last entered value for each patient taken

1 solution

Please, read my comment to the question first.

Try this:
SQL
SELECT t1.PatientID, t2.Height, t1.MeasuredDate, t1.BirthDate, t2.Weight
FROM (
    SELECT PatientID, BirthDate, MONTH(MeasuredDate), MAX(MeasuredDate) AS MeasuredDate
    FROM PatiensTbl
    GROUP BY PatientID, BirthDate, MONTH(MeasuredDate)
    ) AS t1 INNER JOIN PatiensTbl AS t2 ON t1.PatientId = t2.PatientId AND t1.MeasuredDate = t2.MeasuredDate
 
Share this answer
 
v2
Comments
rajin kp 20-Dec-13 4:37am    
tks Maciej
Maciej Los 20-Dec-13 6:31am    
You're welcome ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900