SQL resolution order question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Monroeski
    New Member
    • Aug 2007
    • 30

    SQL resolution order question

    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 -

    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
    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?
  • Monroeski
    New Member
    • Aug 2007
    • 30

    #2
    Nevermind, we figured it out, but I thought it would be a good idea to post the answer up here.

    Basically, in "Question1" , it is returning a True or False when deciding if the field is equal to pain or not. So, it isn't finding the max of the 0s and 1s, it is finding the max of Trues and Falses. So, both questions are evaluating the Max of the initial table fields, not alternating between the table fields and the results as we thought at first.

    Comment

    Working...