如何在SQL的CASE STATEMENT中放入三个条件?

时间:2015-03-06 19:37:56

标签: sql case

这是我的SQL查询:

SELECT *
,CASE WHEN (ETA>10 OR Class='EOL') THEN 'Eligible' ELSE 'Not Applicable' END AS Eligible
FROM XYZtable

结果是:

PN  PD  ETA Class   BU  Serial  Comm    frmToday    Eligible
CX  CRD 03-10   IP  07  2322    CARD    3   Not Applicable
BX  CRD 03-18   IP  05  2324    CARD    11  Eligible
MX  CRD 03-10   EOL 07  2325    CARD    3   Not Applicable

我想在Eligible列中提及以下结果:

If ETA > 10 THEN 'Eligible'
If Class = 'EOL' THEN 'Consult Plz'
Else 'Not Applicable'

感谢。

2 个答案:

答案 0 :(得分:5)

SELECT *,
      CASE WHEN ETA>10 THEN 'Eligible' 
      WHEN Class = 'EOL' THEN 'Consult Plz' 
      ELSE 'Not Applicable' END AS Eligible
FROM XYZtable

答案 1 :(得分:1)

CASE 
    WHEN <condition> THEN <value>
    WHEN <condition> THEN <value>
    WHEN <condition> THEN <value>
    WHEN <condition> THEN <value>
    ELSE <value>
END

或者如果你需要筑巢,你可以

CASE 
    WHEN <condition> THEN 
        CASE <othercondition> THEN <value>
        ELSE <othervalue> END
END

CASE

CASE 
    WHEN ETA > 10 
        THEN 'Eligible' 
    WHEN Class = 'EOL' 
        THEN 'Consult Plz'
    ELSE 'Not Applicable' 
END