on 2022 Nov 14 6:55 PM
I am trying to capture a number value based on when two times occurred. So I need the value that was recorded for the LAST meas.RECORDED_TIME that occurred before the Administration Time of a Medication. I am using the function 'Max' for my meas.RECORDED_TIME but I am not sure how to add in my Medication Time to pull the number I need ???
--Pain Score FLO ID = 18
LEFT OUTER JOIN
(
select distinct ifr.pat_id,ifmwdt.MEAS_VALUE as Pain_Score
from IP_FLWSHT_REC ifr
inner join (SELECT distinct rec.pat_id,max(meas.RECORDED_TIME) as Pain_Score
from ip_flwsht_rec rec
inner join [CLARITY].[dbo].[IP_FLWSHT_MEAS] meas on rec.fsd_id=meas.FSD_ID
where meas.flo_meas_id='18'
and meas.MEAS_VALUE is not null
group by rec.pat_id) mdt on ifr.pat_id=mdt.pat_id
inner join IP_FLWSHT_MEAS ifmwdt on ifr.FSD_ID=ifmwdt.FSD_ID and mdt.Pain_Score=ifmwdt.RECORDED_TIME
where ifmwdt.FLO_MEAS_ID='18' and ifmwdt.MEAS_VALUE is not null
)fmidwt on PEH.PAT_ID =fmidwt.PAT_ID
I'm seeing a number of issues in your SQL, so it's going to take me a bit to fix them in order to simplify the query for you. If I understand you correctly, you're looking for the pain score from the record that has the max timestamp that is less than the administration time. Is that correct? Or is it the first pain score after the administratiof of pain med?
Also, are you showing only the most recent administration or are you showing a set of them over time and need the pain score that is related to each administration?
Is the pain scale where FLW_MEAS.FLO_MEAS_ID = 18? Or is it one of the list of ID's in the query? And which table.field is the administration time?
Thanks!
-Dell
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I had support look at my query and he suggested some CTE's to capture what I was looking for. I am not sure how to use these in the main query to get what I need ? The first part of the query are the CTE's.ctes.txt
You would use the CTE as you would any other table - just join them into your query instead of going out to the tables every time. The CTEs defined in the "with" clause will load that data into memory once and then the data in memory will be used in the larger query instead of going to the tables every time. Also, you would use a CTE in place of the sub-select for the same data in your original query.
-Dell
You reference them as you would any other table you add to your query. You'll define the CTEs above the Select for your main query. In this case they're like this:
With adminInfo as (
...
),
pain1 as (
...
),
pain2 as (
...
)
So you would then add them to the From clause of your query, joining them as you would any other table:
Select
<fields>
From PAT_ENC_HSP PEH
Left outer join adminInfo ai
on ...
Left outer join pain1 p1
on ...
<etc.>
-Dell
User | Count |
---|---|
66 | |
10 | |
10 | |
10 | |
10 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.