简体   繁体   中英

SQL Case when at least one value equals value then set to value

I am trying to update a table by querying distinct values of a professor's degrees. The faculty table has a terminal degree field (Value Y or N). Professors can have more than one degree, but if at least one row exists where they have a terminal degree = Y, I want to set the value to 'Terminal'. If there are only values of 'N', and no values of 'Y' then I want to set terminal degree to 'Not terminal'

Below is the code i currently have, but it doesn't work for professors with more than one degree (Y or N values)

 UPDATE SNP_FACULTY_CENSUS SFC
    SET TERMINAL_DEGREE = (  
        Case
            WHEN EXISTS (SELECT DISTINCT(F.TERMINAL_DEGREE) 
                             FROM FACULTY F
                                left JOIN SNP_FACULTY_CENSUS S
                                ON F.PERSON_SKEY = S.PERSON_sKEY
                             )
                    THEN (SELECT  -- USE TERMINAL VALUE FROM FACULTY TABLE
                           CASE
                                 WHEN F.TERMINAL_DEGREE = 'Y' AND F.TERMINAL_DEGREE = ''--if at least ONE value equals Y
                                 THEN
                                     'Terminal'
                                WHEN F.TERMINAL_DEGREE = 'Y' -- if all values equal Y
                                 THEN 'Terminal'     
                                 WHEN F.TERMINAL_DEGREE = 'N' --if ALL values equal N    
                                 THEN
                                     'Not terminal'
                                 ELSE
                                     'Unknown'
                            END
                          FROM FACULTY F
                            left JOIN SNP_FACULTY_CENSUS S
                                ON F.PERSON_SKEY = S.PERSON_sKEY)                        
        
    
        END 
     ) 
    WHERE SFC.OIR_FALL_TERM = 'Fall 2019'
    ;

You have to use not exists, below query should work, since I dont have the sample data, I am unable to test it:

UPDATE SNP_FACULTY_CENSUS SFC
SET TERMINAL_DEGREE = (  
    Case
        WHEN EXISTS (SELECT DISTINCT F.TERMINAL_DEGREE 
                         FROM FACULTY F
                            left JOIN SNP_FACULTY_CENSUS S
                            ON F.PERSON_SKEY = S.PERSON_sKEY
                         )
                THEN (SELECT  -- USE TERMINAL VALUE FROM FACULTY TABLE
                       CASE
                             WHEN F.TERMINAL_DEGREE = 'Y' --if ONE value equals Y
                             THEN
                                 'Terminal'
                             WHEN F.TERMINAL_DEGREE = 'N' and not exists(select 1 from  FACULTY fa where F.PERSON_SKEY = fa.PERSON_sKEY and Fa.TERMINAL_DEGREE = 'N')--if ALL values equal N    
                             THEN
                                 'Not terminal'
                             ELSE
                                 'Unknown'
                        END
                      FROM FACULTY F
                        left JOIN SNP_FACULTY_CENSUS S
                            ON F.PERSON_SKEY = S.PERSON_sKEY)                        
    

    END 
 ) 
WHERE SFC.OIR_FALL_TERM = 'Fall 2019';

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