查找每个部门的emp_names,最高,最低薪水和雇员人数?

时间:2019-03-30 14:42:39

标签: sql oracle ora-00918

我使用oracle 11g,所以我有2个表(员工,部门):

GET metricbeat*/_search?
{"query": {
          "bool": {
                "must": [
                    { "wildcard" : { "beat.hostname" : "ibmcx*" }},
                    { "range": {
                      "@timestamp": {
                        "gte": "2019-03-22T00:00:00",
                        "lte": "2019-03-23T00:00:00"}}},
                    {"terms" : { "beat.hostname" : ["ibmcxapp101", "ibmcxapp102", "ibmcxapp103",
                                    "ibmcxapp104", "ibmcxapp105", "ibmcxapp106", "ibmcxapp107",
                                    "ibmcxapp108", "ibmcxapp109", "ibmcxapp110", "ibmcxapp111",
                                    "ibmcxapp112", "ibmcxapp113", "ibmcxapp114", "ibmcxapp115",
                                    "ibmcxapp116", "ibmcxapp117", "ibmcxapp118", "ibmcxapp119",
                                    "ibmcxapp120", "ibmcxapp121", "ibmcxapp122", "ibmcxxaa100",
                                    "ibmcxxaa101", "ibmcxxaa102", "ibmcxxaa103", "ibmcxxaa104",
                                    "ibmcxxaa105", "ibmcxxaa106", "ibmcxxaa107", "ibmcxxaa108",
                                    "ibmcxxaa109", "ibmcxxaa110", "ibmcxxaa111", "ibmcxxaa112",
                                    "ibmcxxaa201", "ibmcxxaa202", "ibmcxxaa203", "ibmcxxaa204"
                                    ] }},
                    {"exists": {"field": "system.process.cmdline"}}
                ],
                "must_not": [
                   {"term" : { "system.process.username" : "NT AUTHORITY\\SYSTEM" }},
                   {"term" : { "system.process.username" : "NT AUTHORITY\\NETWORK SERVICE" }},
                   {"term" : { "system.process.username" : "NT AUTHORITY\\LOCAL SERVICE" }},
                   {"term" : { "system.process.username" : "NT AUTHORITY\\Servicio de red"}},
                   {"term" : { "system.process.username" : "" }}
                  ]
          }
        },
        "size": 0,
        "aggs": {
          "group_by_start_time": {
            "terms": {
              "field": "system.process.cpu.start_time"
            },
            "aggs": {
              "group_by_name": {
                "terms": {
                  "field": "system.process.name.keyword"
                }
              }
            }
          }
        },
        "size": 0,
        "sort" : [
            { "system.process.cpu.start_time" : {"order" : "asc"}},
            { "@timestamp" : {"order" : "asc"}},
            { "system.process.pid" : {"order" : "desc"}}
        ]}

我想得到

  

雇员姓名,emp_names,emp_salary,dep_id,dep_names,每个人的最高工资   dep和每个dep的最低工资,以及每个dep的雇员人数   部门。

所以我要这样做:

desc employees:      desc departments

 EMPLOYEE_ID        DEPARTMENT_ID
 FIRST_NAME         DEPARTMENT_NAME
 LAST_NAME          MANAGER_ID
 EMAIL              LOCATION_ID
 PHONE_NUMBER
 HIRE_DATE
 JOB_ID
 SALARY
 COMMISSION_PCT
 MANAGER_ID
 DEPARTMENT_ID

但是它给出了一个错误:

  

第1行出现错误:ORA-00918:列定义不明确

但是我的sql查询正确吗?

1 个答案:

答案 0 :(得分:0)

我没有您的桌子,所以我用Scott的视图创建了视图,以模拟您的视图。

SQL> create or replace view employees as
  2    select empno  employee_id,
  3           ename  last_name,
  4           deptno department_id,
  5           sal    salary
  6    from emp;

View created.

SQL> create or replace view departments as
  2    select deptno department_id,
  3           dname  department_name
  4    from dept;

View created.

SQL>

这就是我对问题的理解:每个部门的员工名单应与其余部门分开(最小,最大,人数)。

所以:雇员名单:

SQL> select d.department_name, e.last_name
  2  from departments d join employees e on d.department_id = e.department_id
  3  order by d.department_name;

DEPARTMENT_NAM LAST_NAME
-------------- ----------
ACCOUNTING     CLARK
ACCOUNTING     KING
ACCOUNTING     MILLER
RESEARCH       JONES
RESEARCH       FORD
RESEARCH       ADAMS
RESEARCH       SMITH
RESEARCH       SCOTT
SALES          WARD
SALES          TURNER
SALES          ALLEN
SALES          JAMES
SALES          BLAKE
SALES          MARTIN

14 rows selected.

集合:没有员工的部门的外部联接:

SQL> select d.department_name,
  2    min(e.salary) min_sal,
  3    max(e.salary) max_sal,
  4    count(e.employee_id) cnt_emp
  5  from departments d left join employees e on d.department_id = e.department_id
  6  group by d.department_name
  7  order by d.department_name;

DEPARTMENT_NAM    MIN_SAL    MAX_SAL    CNT_EMP
-------------- ---------- ---------- ----------
ACCOUNTING           1300       5000          3
OPERATIONS                                    0
RESEARCH              800       3000          5
SALES                 950       2850          6

LISTAGG允许您在同一语句中列出每个部门的所有员工;参见第5行。我以某种方式怀疑您是否已经了解了该功能(当您为此类问题而苦苦挣扎时)。

SQL> select d.department_name,
  2    min(e.salary) min_sal,
  3    max(e.salary) max_sal,
  4    count(e.employee_id) cnt_emp,
  5    listagg(e.last_name, ', ') within group (order by e.last_name) employees
  6  from departments d left join employees e on d.department_id = e.department_id
  7  group by d.department_name
  8  order by d.department_name;

DEPARTMENT_NAM    MIN_SAL    MAX_SAL    CNT_EMP EMPLOYEES
-------------- ---------- ---------- ---------- -------------------------------------------
ACCOUNTING           1300       5000          3 CLARK, KING, MILLER
OPERATIONS                                    0
RESEARCH              800       3000          5 ADAMS, FORD, JONES, SCOTT, SMITH
SALES                 950       2850          6 ALLEN, BLAKE, JAMES, MARTIN, TURNER, WARD

SQL>