Quantcast
Channel: PHP Freaks: PHP Help
Viewing all articles
Browse latest Browse all 13200

qsl : datetime that X last became true (and stayed true)

$
0
0

Hi all, im using the following sql query to select data from two tables where the most recent value for 'par' is > 6. 

SELECT patients.*, addobs.*, DATE_FORMAT(addobs.datetime, '%d/%m/%Y %H:%i:%s') as dti1 
FROM addobs INNER JOIN patients ON addobs.MRN = patients.MRN
WHERE addobs.datetime = (SELECT MAX(OLAST.datetime) FROM addobs AS OLAST WHERE OLAST.MRN = patients.MRN)
AND addobs.par > 6 
AND NOT addobs.hidden = 'yes'
AND COALESCE(patients.ward,'') != 'dc'
order by addobs.par ASC

I would like to extend this query to also return as 'dti2', the datetime value that par > 6 LAST BECAME true.

 

To clarify, if i have 5 values as follows:

 

          datetime                   par

06-18-2013 05:00:00           7

06-18-2013 04:00:00           8

06-18-2013 03:00:00           7

06-18-2013 02:00:00           2

06-18-2013 01:00:00          10

 

Then the datetime to be returned by my query as dti2 would be 06-18-2013 03:00:00, as this is the time that par>6 LAST BECAME true (and it has STAYED true since this time)

 

This is to be distinguished from 06-18-2013 01:00:00 (which is when PAR>6 FIRST became true)

and also to be distinguished from 06-18-2013 05:00:0 (which is when PAR>6 was LAST true)

 

To give some context for further clarity: PAR is a measure of how unwell a patient is (larger numbers = more unwell). So i am trying to create a list of unwell patients. I want to return patients who have a PAR score of >6 (which is what my query currently achieves) and the amount of time they have had CONSECUTIVE par scores >6.

 

From my limited knowledge of sql i would have thought that this could be done somehow with coalesce but im not sure exactly how. i have tried a few different things with no luck.

 

I hope that all makes sense, i get a little confused even explaining it!

 

Thanks for any help anyone can give in advance,

 

Best,

 

Matt


Viewing all articles
Browse latest Browse all 13200

Trending Articles