简体   繁体   中英

SQL Case Statement not working with 'Else'

The following Case statements do not work correctly - producing all results as 'i' (the 'Else' part of the statement). If I comment out the Else parts the code works ok, producing NULLs where the criteria isn't met. Any ideas why this is happening? Is ELSE not supported? I'm sure I've seen Else used in Case statements (but never with And )?

SELECT 
        EN,
        (First_Name + ' ' + Last_Name) as Name,

        First_Aid =MAX(CASE
            WHEN Course = 'First Aid' 
            and Status = 'Finished'
            and Course_Start_Date > DATEADD(day, -1095, GETDATE())
            THEN 'F'
            ELSE 'i'
            END),

        Manual_Handling =MAX(CASE
            WHEN Course = 'Manual Handling' 
            and Status = 'Finished'
            and Course_Start_Date > DATEADD(day, -1095, GETDATE())
            THEN 'F'
            ELSE 'i'
            END),

        Fire_Safety =MAX(CASE
            WHEN Course = 'Fire Safety' 
            and Status = 'Finished'
            and Course_Start_Date > DATEADD(day, -1095, GETDATE())
            THEN 'F'
            ELSE 'i'
            END)
into MyTraining
FROM Learning_History

Group By EN,First_Name, Last_Name

As GSerg commented, you're asking for the wrong thing. You probably want to use a query like this instead:

Fire_Safety =MAX(CASE
WHEN Course = 'Fire Safety' 
and Status = 'Finished'
and Course_Start_Date > DATEADD(day, -1095, GETDATE())
THEN Course_Start_Date
ELSE null
END)

This will give you the nearest day of the fire safety course, or null if there's none that satisfy the condition. You can then have another case around the max if needed to return either T or F or whatever you want based on the value returned. Or not, it's not really clear what you want to happen in either case :)

I think you want to pull the assignment to I and F outside the aggregation. Here is one method:

SELECT EN,
       (First_Name + ' ' + Last_Name) as Name,
       (CASE WHEN SUM(CASE WHEN Course = 'First Aid' and Status = 'Finished' and Course_Start_Date > DATEADD(day, -1095, GETDATE())
                           THEN 1 ELSE 0 END) > 0
             THEN 'F' ELSE 'I'
        END) as First_Aid,
       (CASE WHEN SUM(CASE WHEN course = 'Manual Handling' and Status = 'Finished' and Course_Start_Date > DATEADD(day, -1095, GETDATE()) > 0
                          THEN 1 ELSE 0 END) > 0
             THEN 'F' ELSE 'I'
        END) as Manual_Handling,
       (CASE WHEN SUM(CASE WHEN Course = 'Fire Safety' and Status = 'Finished' and Course_Start_Date > DATEADD(day, -1095, GETDATE())
                               THEN 1 ELSE 0 END) > 0
            THEN 'F'
            ELSE 'i'
        END) as Fire_Safety
into MyTraining
FROM Learning_History
Group By EN, First_Name, Last_Name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM