为什么这个'如果'逻辑无法在SQL存储过程中工作?

时间:2014-07-05 04:40:09

标签: sql sas

我想在SQL存储过程中输入以下逻辑:

IF(I2<=13.5,"<=13.5%",
   IF(I2<=14,"13.5% - 14%",
   IF(I2<=14.5,"14% - 14.5%",
   IF(I2<=15,"14.5% - 15%",
   IF(I2<=16,"15% - 16%",
   IF(I2<=18,"16% - 18%",
   IF(I2<=20,"18% - 20%",">20%"))
)

SQL是:

COALESCE(
         Case When t1.irr___current <=13.5  then "<=13.5%" else end,
         Case When t1.irr___current <=14  then "13.5% - 14%" else end,
         Case When t1.irr___current <=14.5  then "14% - 14.5%" else end,
         Case When t1.irr___current <=15  then "14.5% - 15%" else end,
         Case When t1.irr___current <=16  then "15% - 16%" else end,
         Case When t1.irr___current <=18  then "16% - 18%" else end,
         Case When t1.irr___current <=20  then "18% - 20%" else ">20%" end)

为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

尝试这个

  CASE
        WHEN t1.irr___current IS NULL   THEN '???'
        WHEN t1.irr___current <= 13.5   THEN '<=13.5%'
        WHEN t1.irr___current <= 14     THEN '13.5% - 14%'
        WHEN t1.irr___current <= 14.5   THEN '14% - 14.5%'
        WHEN t1.irr___current <= 15     THEN '14.5% - 15%'
        WHEN t1.irr___current <= 16     THEN '15% - 16%'
        WHEN t1.irr___current <= 18     THEN '16% - 18%'
        WHEN t1.irr___current <= 20     THEN '18% - 20%'
        ELSE '>20%'
  END