I've been using SQL for a while now, but nothing too detailed, so I signed up for an SQL class at work. A question I had about a certain query, though, has brought everything to a screeching halt while we search for an answer. Here's the code -
I changed a few things to make my question a little more readable. Basically, what we're wondering is this - when you are evaluating "Question1" , why does the Max() find the largest number you are inserting (the 0 or 1), whereas in evaluating Question2, it is finding the max of the field you are searching (numresult), and NOT the result you are inserting?
Code:
select vitaldate, ptien, locien, dssid,
1 as vitalpanel,
Question1 = max(case when type = 'pain' then 1 else 0 end),
Question2 = max(case when type = 'pain' and numresult = 99 then 'nonresponsive'
when type = 'pain' and numresult >=0 and numresult <5 then '0-4'
when type = 'pain' and numresult >= 5 and numresult <= 10 then '5-10'
when type = 'pain' and numresult is null then null
when type = 'pain' then 'other' end)
from vitals
group by vitaldate, ptien, locien, dssid
Comment