tags:

views:

93

answers:

4

This query

   SELECT PA.refPatient_id
         ,MAX(PA.datee) AS datee
         ,PR.temporary,PA.statue

     FROM PatientClinicActs AS PA
         ,PatientStatueReasons AS PR

    WHERE PA.refClinic_id = 25 
         AND PA.refreason_id = PR.reason_id

 GROUP BY PA.refPatient_id,PA.statue,PR.temporary

returns these results:

refPatient_id datee                 temporary statue
----------- ----------------------- ------ -----
14706       2008-12-01 11:01:00     1      0
14707       2009-05-18 16:21:00     1      0
14708       2009-07-15 09:46:00     1      0
14708       2009-07-29 16:12:00     1      0
14716       2009-11-09 12:29:00     0      0
14716       2009-09-01 11:15:00     1      0
14716       2009-09-29 16:44:00     1      1

But i want to have these results:

refPatient_id datee                   temporary statue
----------- ----------------------- ------ -----
14706       2008-12-01 11:01:00     1      0
14707       2009-05-18 16:21:00     1      0
14708       2009-07-29 16:12:00     1      0
14716       2009-11-09 12:29:00     0      0

What is the difference ? => I have newest row for every refPatient_id in these results. What should i run to get these results?

A: 

Take the PA.datee out of the group by clause

Sparky
It did not work.
uzay95
+3  A: 

Try something like this

SELECT  PA.refPatient_id,
     PA.datee,
     PR.temporary,
     PA.statue
FROM    PatientClinicActs AS PA INNER JOIN
     (
      SELECT PA.refPatient_id,
        MAX(PA.datee) AS datee
      FROM PatientClinicActs AS PA
      WHERE PA.refClinic_id = 25          
      GROUP BY PA.refPatient_id,PA.statue,PA.datee,
     ) AS MaxDates ON PA.refPatient_id = MaxDates.refPatient_id AND PA.datee = MaxDates.datee INNER JOIN
     PatientStatueReasons AS PR ON PA.refreason_id = PR.reason_id

You need to get the max date per patient first, then join to that.

astander
It still has more than one row for some patients.
uzay95
Tha might be due to either the MAX date occuring more than once in PatientClinicActs or the link to PatientStatueReasons containing more than one entry for a patient, in which case you must decide how to choose the appropriate record. What will make it the one you need?
astander
I voted you down because I was misreading sth... Cannot undo it apparently, so I voted you up on 'Retrieving rows with the highest value'for fairness
Peter
Ok. if i retrive refPatient_id, refReason_id and datee, i could have more than 1 actions for some patients. Because they might be passive (statue=0) and then active(statue=1) with some reasons.
uzay95
Then change it to select only the statue you require 0 or 1
astander
Good, but i need to know what this patient did (leave/come) with his/her last movement...
uzay95
Provide the table structure you have and some data, and that will make things easier.
astander
@Peter: You can't reverse a downvote until after the answer has been editted.
OMG Ponies
+1  A: 

Instead of a cross join

FROM PatientClinicActs AS PA
                ,PatientStatueReasons AS PR

can you try with inner join

FROM PatientClinicActs AS PA

INNER JOIN PatientStatueReasons AS PR 

ON PA.refreason_id = PR.reason_id

WHERE PA.refClinic_id = 25
priyanka.sarkar
+1 for good practice even if it doesn't solve it...
gbn
Thank you very much sir. I have learnt a lot from you and is still learning. Your comments are always valuable to me. Thanks again
priyanka.sarkar
It's not a cross join - it's an inner join using non ANSI JOIN syntax.
OMG Ponies
A: 

For this kind of cases, there is another way to get the latest record by using SQL Server Ranking functions.

I have used DENSE_RANK() for my answer, but you can use RANK(), instead for your particular problem.
(note: code below is not tested. If you were to provide schema for tables, I would have tested with sample data)

;with RankedResult as (
 SELECT 
 PA.refPatient_id
 , PA.datee
 , PR.temporary,
 , PA.statue
 --; Last datee has the lowest rank value of 1,
 , dense_rank() over 
  (partition by PA.refPatient_id order by PA.datee desc) as [Rank]
 FROM PatientClinicActs AS PA
   join PatientStatueReasons AS PR on PA.refreason_id = PR.reason_id
 WHERE PA.refClinic_id = 25 
)
select distinct *
from RankedResult
--; Get only the last result.
where [Rank] = 1
Sung Meister