显示员工姓名,每位员工的总薪水(总薪水=工资+佣金)

时间:2014-06-07 08:49:14

标签: sql

显示员工姓名,每位员工的总薪水。

条件:

  • 如果佣金为not NULL,则为total salary=(salary + commission)
  • else total salary = salary;

这是我的表:

表名:myemp

列:

empno [Primary key], name, salary, commission, deptno

create table myemp
( 
     empno number primary key,
     name varchar2(20),
     salary number,
     commission number,
     deptno number
);

查询:

select
    name, salary, commission,
    (salary + ((salary*commission) / 100)) as "total_salary" 
from myemp;

此查询提供的结果不正确。

佣金为NULL时,total_salary = 0。但它应该是total_salay=salary;

如何解决这个问题?

6 个答案:

答案 0 :(得分:1)

我的表如下: SQL> select * from emp;

 EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
  7369 SMITH      CLERK           7902 17-DEC-80        800                    20
  7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
  7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
  7566 JONES      MANAGER         7839 02-APR-81       2975                    20
  7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
  7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
  7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
  7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
  7839 KING       PRESIDENT            17-NOV-81       5000                    10
  7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
  7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
  7900 JAMES      CLERK           7698 03-DEC-81        950                    30
  7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
  7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

选择了14行。

我的结果表,而我在comm列中的空值是:

SQL>选择empno,ename,sal,comm,(sal + nvl(comm,0))作为emp的“Total_sal”;

 EMPNO ENAME             SAL       COMM  Total_sal
  7369 SMITH             800                   800
  7499 ALLEN            1600        300       1900
  7521 WARD             1250        500       1750
  7566 JONES            2975                  2975
  7654 MARTIN           1250       1400       2650
  7698 BLAKE            2850                  2850
  7782 CLARK            2450                  2450
  7788 SCOTT            3000                  3000
  7839 KING             5000                  5000
  7844 TURNER           1500          0       1500
  7876 ADAMS            1100                  1100
  7900 JAMES             950                   950
  7902 FORD             3000                  3000
  7934 MILLER           1300                  1300

选择了14行。

答案 1 :(得分:0)

(salary + ((salary*coalesce(commission,0) / 100)) as "total_salary" 

答案 2 :(得分:0)

查询:

 select
    name, salary, commission,
    (salary + coalesce(((salary * commission) / 100), 0)) "total_salary" 
 from myemp;

答案 3 :(得分:0)

你正在寻找工资+佣金,如果有的话。问题不在于如何有条件地增加佣金,而在于增加多少薪水。您知道结果将至少是员工的初始薪水,因此如果佣金不存在,那么只需添加0作为佣金。

Select name, (salary + IsNull(commission, 0)) as total_salary

答案 4 :(得分:0)

选择工资月作为总计,按员工组计算()按总计顺序通过总计desc限制1;

由...创建 Rahul Panwar

答案 5 :(得分:0)

select name,salary,commission,nvl(salary,0)+nvl(commission,0) as Total_salary from myemp;