查询hr模式

时间:2015-03-25 18:12:15

标签: sql oracle

我是SQL新手,正在练习Oracle 10g XE上提供的HR模式。 这是个问题: 编写一个查询,从部门编号80中选择除销售代表之外的所有员工的姓名,职位,工资和部门编号。

我的疑问是:

select first_name||' '||last_name "Employee Name"
     , job_id, salary, department_id
  from employees
 where not( job_id='SA_REP' and department_id=80 )
 order by department_id;

输出:

Employee Name                                  JOB_ID         SALARY DEPARTMENT_ID
---------------------------------------------- ---------- ---------- -------------
Jennifer Whalen                                AD_ASST          4400            10
Michael Hartstein                              MK_MAN          13000            20
Pat Fay                                        MK_REP           6000            20
Den Raphaely                                   PU_MAN          11000            30
Alexander Khoo                                 PU_CLERK         3100            30
Shelli Baida                                   PU_CLERK         2900            30
Sigal Tobias                                   PU_CLERK         2800            30
Guy Himuro                                     PU_CLERK         2600            30
Karen Colmenares                               PU_CLERK         2500            30
Susan Mavris                                   HR_REP           6500            40
Matthew Weiss                                  ST_MAN           8000            50
Adam Fripp                                     ST_MAN           8200            50
Payam Kaufling                                 ST_MAN           7900            50
Shanta Vollman                                 ST_MAN           6500            50
Kevin Mourgos                                  ST_MAN           5800            50
Julia Nayer                                    ST_CLERK         3200            50
Irene Mikkilineni                              ST_CLERK         2700            50
James Landry                                   ST_CLERK         2400            50
Steven Markle                                  ST_CLERK         2200            50
Laura Bissot                                   ST_CLERK         3300            50
Mozhe Atkinson                                 ST_CLERK         2800            50
James Marlow                                   ST_CLERK         2500            50
TJ Olson                                       ST_CLERK         2100            50
Jason Mallin                                   ST_CLERK         3300            50
Michael Rogers                                 ST_CLERK         2900            50
Ki Gee                                         ST_CLERK         2400            50
Hazel Philtanker                               ST_CLERK         2200            50
Renske Ladwig                                  ST_CLERK         3600            50
Stephen Stiles                                 ST_CLERK         3200            50
John Seo                                       ST_CLERK         2700            50
Joshua Patel                                   ST_CLERK         2500            50
Trenna Rajs                                    ST_CLERK         3500            50
Curtis Davies                                  ST_CLERK         3100            50
Randall Matos                                  ST_CLERK         2600            50
Peter Vargas                                   ST_CLERK         2500            50
Winston Taylor                                 SH_CLERK         3200            50
Jean Fleaur                                    SH_CLERK         3100            50
Martha Sullivan                                SH_CLERK         2500            50
Girard Geoni                                   SH_CLERK         2800            50
Nandita Sarchand                               SH_CLERK         4200            50
Alexis Bull                                    SH_CLERK         4100            50
Julia Dellinger                                SH_CLERK         3400            50
Anthony Cabrio                                 SH_CLERK         3000            50
Kelly Chung                                    SH_CLERK         3800            50
Jennifer Dilly                                 SH_CLERK         3600            50
Timothy Gates                                  SH_CLERK         2900            50
Randall Perkins                                SH_CLERK         2500            50
Sarah Bell                                     SH_CLERK         4000            50
Britney Everett                                SH_CLERK         3900            50
Samuel McCain                                  SH_CLERK         3200            50
Vance Jones                                    SH_CLERK         2800            50
Alana Walsh                                    SH_CLERK         3100            50
Kevin Feeney                                   SH_CLERK         3000            50
Donald OConnell                                SH_CLERK         2600            50
Douglas Grant                                  SH_CLERK         2600            50
Alexander Hunold                               IT_PROG          9000            60
Bruce Ernst                                    IT_PROG          6000            60
David Austin                                   IT_PROG          4800            60
Valli Pataballa                                IT_PROG          4800            60
Diana Lorentz                                  IT_PROG          4200            60
Hermann Baer                                   PR_REP          10000            70
John Russell                                   SA_MAN          14000            80
Karen Partners                                 SA_MAN          13500            80
Alberto Errazuriz                              SA_MAN          12000            80
Gerald Cambrault                               SA_MAN          11000            80
Eleni Zlotkey                                  SA_MAN          10500            80
Steven King                                    AD_PRES         24000            90
Neena Kochhar                                  AD_VP           17000            90
Lex De Haan                                    AD_VP           17000            90
Nancy Greenberg                                FI_MGR          12000           100
Daniel Faviet                                  FI_ACCOUNT       9000           100
John Chen                                      FI_ACCOUNT       8200           100
Ismael Sciarra                                 FI_ACCOUNT       7700           100
Luis Popp                                      FI_ACCOUNT       6900           100
Jose Manuel Urman                              FI_ACCOUNT       7800           100
Shelley Higgins                                AC_MGR          12000           110
William Gietz                                  AC_ACCOUNT       8300           110

77 rows selected.


I am not however getting the desired output,as the output is not displaying this row:

Employee Name                                  JOB_ID         SALARY DEPARTMENT_ID
---------------------------------------------- ---------- ---------- -------------
Kimberely Grant                                SA_REP           7000

此记录没有部门编号,但也应与其他77行一起显示在输出中。任何人都可以指出我的查询出错了吗?

3 个答案:

答案 0 :(得分:0)

所以我们的目标是获得除销售代表和80部门以外的所有员工的清单?也就是说,你仍然希望部门80(以及所有其他部门)的员工除非他们是销售代表和销售代表,除非他们在部门80?

在Oracle中,NULL值与其他任何值的比较都会返回NULL而不是TRUEFALSE。因此,您需要:(a)添加明确的IS NULL条件(或IS NOT NULL,具体取决于您的查询参数)或(b)使用COALESCE()(或{{1功能。像这样:

NVL()

或:

SELECT first_name || ' ' || last_name "Employee Name"
     , job_id, salary, department_id
  FROM employees
 WHERE job_id != 'SA_REP'
    OR department_id IS NULL
    OR department_id != 80
 ORDER BY department_id;

答案 1 :(得分:0)

感谢@DavidFaber确认我最初的想法。你在where子句中的不是没有与department_id = 80部分一起玩,因为在department_id中Kimbereley Grant的空值。基本上,"不是(job_id =' SA_REP'和department_id = 80)"变成"而不是job_id =' SA_REP'或者不是department_id = 80",因此" job_id!=' SA_REP'或department_id!= 80"。

Kimberely Grant未通过第一次检查(她的job_id实际上是SA_REP),第二次检查应该基本上导致"未知" (她的department_id不是80,但它也不是80,它是NULL)。结果,你有假或"未知"这变得虚假。

修复是@Ponder建议的。在department_id上添加空检查:

not(job_id =' SA_REP' and department_id = 80且department_id不为空)

相当于:

not(job_id =' SA_REP' and department_id = 80)或department_id为空

答案 2 :(得分:0)

谢谢大家。我现在可以使用此查询获得所需的输出:

SELECT first_name || ''|| last_name“员工姓名”      ,job_id,salary,department_id   来自员工  WHERE job_id!='SA_REP'     OR department_id是NULL     OR department_id!= 80  ORDER BY department_id;

非常感谢。谢谢。