2014-06-07 75 views
0

顯示員工姓名,每位員工的總工資。顯示員工姓名,每位員工的總工資(總工資=工資+佣金)

條件:

  • 如果佣金是not NULL然後total salary=(salary + commission)
  • 其他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;

如何解決這個問題?

+0

的就是你得到的結果,什麼是你所期望的結果呢?您可以添加一個示例以便更好地理解 – Jens

回答

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

查詢:

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

您正在尋找的工資+提成,如果有一個。問題不在於如何有條件地增加佣金,而在於增加多少薪水。你知道結果至少是員工的最初薪水,所以如果佣金不存在,那麼只需加上0作爲佣金。

Select name, (salary + IsNull(commission, 0)) as total_salary 
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行。

0

select salary 10 months as total total,count()from Employee group by totalearing order by totalearing desc limit 1;

通過 拉胡爾Panwar創建

+0

我們將員工的總收入定義爲他們的月度工作,並將最大總收入定義爲員工表中任何員工的最大總收入。編寫查詢以查找所有員工的最大總收入以及總收入最高的員工總數。然後以空格分隔的整數形式打印這些值。 –

+0

select salary/months as totalearing,count()from Employee group by totalearing order by totalearing desc limit 1; –

相關問題