通过使用解码来显示员工等级的SQL查询

时间:2013-08-13 11:59:18

标签: sql

我有一个表“Emp”,如下所示

enter image description here

我想添加另一个列,描述所有员工的成绩

Sal> 100是'G1',

Sal> 200是'G2',

Sal> 300是'G3',

全部为“NA”

我想使用“Decode”命令来解决此问题。请帮我解决SQL查询

enter image description here

3 个答案:

答案 0 :(得分:2)

select 
id,name,sal,
case when sal between 100 and 199 then 'G1'
when sal between 200 and 299 then 'G2'
when sal>=300 then 'G3'
else 'NA' end as grade
from Emp

答案 1 :(得分:1)

感谢大家帮助我找到答案。

SELECT Name,
       Sal,
       DECODE (TRUNC ( (sal - 1) / 100),
               0, 'NA',
               1, 'G 1',
               2, 'G 2',
               3, 'G 3')
          AS Grade
  FROM Employee;

答案 2 :(得分:0)

首先,您必须在表格中添加列

Alter table Emp
Add Grade varchar(10)

比您必须更新新字段

update Emp Set Grade= 'G3' where Sal >= 300 
update Emp Set Grade= 'G2' where Sal >= 200 and < 300
update Emp Set Grade = 'G1' where sal >= 100
update Emp Set Grade = 'NA' where sal < 100