根据当前工资更新激励列

时间:2018-05-23 10:06:44

标签: sql sql-server tsql

CREATE TABLE EMPSAL
(
  ID INT ,
  NAME VARCHAR(40),
  SALARY FLOAT,
  INCENTIVES FLOAT  
)

INSERT INTO EMPSAL VALUES (1,'ABHI',500,NULL)
INSERT INTO EMPSAL VALUES (2,'BABY',600,NULL)
INSERT INTO EMPSAL VALUES (3,'CHARAN',3000,NULL)
INSERT INTO EMPSAL VALUES (4,'DHANA',4000,NULL)
INSERT INTO EMPSAL VALUES (5,'EMO',5000,NULL)
INSERT INTO EMPSAL VALUES (6,'FARAN',6000,NULL)
INSERT INTO EMPSAL VALUES (7,'GEO',7000,NULL)
INSERT INTO EMPSAL VALUES (8,'JAHANGEER',8000,NULL)

如果薪水是<

将奖励列更新为(SALARY + 5%SALARY) 1000。

如果薪水是<<<<<<<<< 5000.

如果工资高于6000,则将奖励列更新为SALARY。

我不知道如何启动此查询。

3 个答案:

答案 0 :(得分:1)

只需使用Case,如下所示:

Update EMPSAL
Set Incentive =
Case
    WHEN Salary < 1000 then SALARY * 1.05  
    WHEN Salary < 5000 AND salary >= 1000 THEN SALARY *  1.07
    ELSE SALARY
End

答案 1 :(得分:0)

UPDATE EMPSAL
SET INCENTIVES = (CASE WHEN Salary < 1000 then SALARY * 0.05  
                      WHEN Salary < 5000 AND salary >1000 THEN SALARY *  0.07
                      ELSE SALARY 
                      END)

答案 2 :(得分:0)

这应该在您指定的规则范围内工作(但如果工资在5000到6000之间并且未定义,则不会有差距:

UPDATE EMPSAL
SET INCENTIVES = CASE 
        WHEN SALARY < 1000
            THEN SALARY + (SALARY * 0.05)
        WHEN SALARY BETWEEN 1000 AND 5000
            THEN SALARY + (SALARY * 0.07)
        WHEN SALARY >= 6000
            THEN SALARY
        END