以下表达式无法正常工作,实际上我想基于以下条件使用案例
更新sal和desgSELECT SAL,
CASE SAL WHEN SAL BETWEEN 3000 AND 5000 THEN SAL=SAL+(0.2*SAL) AND DESG='DEO'
WHEN SAL BETWEEN 5001 AND 7000 THEN SAL=SAL+(.15*SAL) AND DESG='ENGNRR'
WHEN SAL BETWEEN 7001 AND 10000 THEN SAL=SAL+(0.25*SAL) AND DESG='SSE'
WHEN SAL >1000 THEN SAL=SAL +(0.15*SAL) AND DESG='PSE'
` `ELSE SAL=SAL+1500 AND DESG='TL'
END
FROM SAMPLES
我的表名是样本
答案 0 :(得分:1)
首先,您需要UPDATE
个名称来更新sal
。
THEN
之后的Secon只能是一个表达式,而不是AND
个比较器。如果要创建两列,则需要使用两个CASE
expresion
SELECT SAL,
CASE WHEN SAL BETWEEN 3000 AND 5000 THEN SAL+(0.2*SAL) -- == SAL * 1.2
WHEN SAL BETWEEN 5001 AND 7000 THEN SAL+(.15*SAL) -- == SAL * 1.15
WHEN SAL BETWEEN 7001 AND 10000 THEN SAL+(0.25*SAL) -- == SAL * 1.25
WHEN SAL > 1000 THEN SAL +(0.15*SAL) -- == SAL * 1.15
ELSE SAL+1500
END as newSal,
CASE WHEN SAL BETWEEN 3000 AND 5000 THEN 'DEO'
WHEN SAL BETWEEN 5001 AND 7000 THEN 'ENGNRR'
WHEN SAL BETWEEN 7001 AND 10000 THEN 'SSE'
WHEN SAL > 1000 THEN 'PSE'
ELSE 'TL'
END as newDSG
FROM SAMPLES;
此外,您不需要CASE
上的值,您需要并更新
UPDATE SAMPLES
SET SAL = CASE WHEN SAL BETWEEN 3000 AND 5000 THEN SAL+(0.2*SAL)
WHEN SAL BETWEEN 5001 AND 7000 THEN SAL+(.15*SAL)
WHEN SAL BETWEEN 7001 AND 10000 THEN SAL+(0.25*SAL)
WHEN SAL > 1000 THEN SAL +(0.15*SAL)
ELSE SAL+1500
END,
DESG = CASE WHEN SAL BETWEEN 3000 AND 5000 THEN 'DEO'
WHEN SAL BETWEEN 5001 AND 7000 THEN 'ENGNRR'
WHEN SAL BETWEEN 7001 AND 10000 THEN 'SSE'
WHEN SAL > 1000 THEN 'PSE'
ELSE 'TL'
END;
答案 1 :(得分:0)
UPDATE SAMPLES
SET SAL = CASE WHEN SAL BETWEEN 3000 AND 5000 THEN SAL*1.2
WHEN SAL > 5000 AND SAL <= 7000 THEN SAL*1.15
WHEN SAL > 7000 AND SAL <=10000 THEN SAL*1.25
WHEN SAL >10000 THEN SAL*1.15
ELSE SAL+1500
END,
DESG = CASE WHEN SAL BETWEEN 3000 AND 5000 THEN 'DEO'
WHEN SAL > 5000 AND SAL <= 7000 THEN 'ENGNRR'
WHEN SAL > 7000 AND SAL <=10000 THEN 'SSE'
WHEN SAL >10000 THEN 'PSE'
ELSE 'TL'
END;