SQL子查询未返回任何结果

时间:2018-12-06 18:30:09

标签: sql oracle

我正在尝试显示部门中员工人数最多的员工人数。但是查询我没有返回任何输出,它不会失败,但是不会返回任何数据。我正在使用Oracle SQL开发人员,但大多数形式的SQL都可以提供帮助。

对不起,我应该已经指定了,但这是针对一堂课的,老师只希望不加入子查询。

select d.DEPARTMENT_ID, d.DEPARTMENT_NAME, count(e.employee_id)
from departments d, employees e
where d.department_id = (select max(count(employee_id)) from employees group by 
department_id)
group by d.department_id, d.department_name;

3 个答案:

答案 0 :(得分:2)

这可以做到:

with
x as (
  select d.department_id, d.department_name, count(*) as tot
  from employees e
  join departments d on d.department_id = e.department_id
  group by d.department_id, d.department_name
),
y as (
  select max(tot) as t from x
)
select x.* from x join y on y.t = x.tot;

附加功能(价格相同):如果首先绑定两个部门,则此查询将向您显示两个部门。

答案 1 :(得分:2)

您正试图找到与多个员工匹配的部门ID;不是这些员工所属的部门。

如果您使用12c或更高版本,则可以使用行限制子句:

select d.department_id, d.department_name, count(e.employee_id) as employee_count
from departments d
join employees e on e.department_id = d.department_id
group by d.department_id, d.department_name
order by count(e.employee_id) desc
fetch first row only;

DEPARTMENT_ID DEPARTMENT_NAME                EMPLOYEE_COUNT
------------- ------------------------------ --------------
           50 Shipping                                   45

如果可以打结,并且想要显示所有绑定的行,请将only更改为with ties;否则,您会从那些并列的行中得到不确定的行。

在早期版本中,等效项为:

select department_id, department_name, employee_count
from (
  select d.department_id, d.department_name, count(e.employee_id) as employee_count
  from departments d
  join employees e on e.department_id = d.department_id
  group by d.department_id, d.department_name
  order by count(e.employee_id) desc
)
where rownum = 1;

DEPARTMENT_ID DEPARTMENT_NAME                EMPLOYEE_COUNT
------------- ------------------------------ --------------
           50 Shipping                                   45

如果有关系也不确定。

要在12c之前正确处理领带,可以使用类似于@Littlefoot的查询,但要使用rank()

select department_id, department_name, employee_count
from (
  select d.department_id, d.department_name, count(e.employee_id) as employee_count,
    rank() over (order by count(*) desc) as rnk
  from departments d
  join employees e on e.department_id = d.department_id
  group by d.department_id, d.department_name
  order by count(e.employee_id) desc
)
where rnk = 1;

...或使用@TheImpaler的方法。


  

老师只想要子查询

受到人为限制总是很有趣...但是您可以这样做:

select d.department_id, d.department_name, count(e.employee_id) as employee_count
from departments d
join employees e on e.department_id = d.department_id
group by d.department_id, d.department_name
having count(e.employee_id) = (
  select max(count(employee_id))
  from employees
  group by department_id
);

将显示联系;或

select d.department_id, d.department_name, count(e.employee_id) as employee_count
from departments d
join employees e on e.department_id = d.department_id
where d.department_id in (
  select department_id
  from (
    select department_id
    from employees
    group by department_id
    order by count(employee_id) desc
  )
  where rownum = 1
)
group by d.department_id, d.department_name;

除非您再次将rownum更改为rank(),否则不会。

  

如果可能的话,不进行显式连接

如果不允许您任何加入,我想您可以这样做:

select d.department_id, d.department_name,
  (select count(e.employee_id)
    from employees e
    where e.department_id = d.department_id) as employee_count
from departments d
where d.department_id in (
  select department_id
  from (
    select department_id
    from employees
    group by department_id
    order by count(employee_id) desc
  )
  where rownum = 1
)
group by d.department_id, d.department_name;

DEPARTMENT_ID DEPARTMENT_NAME                EMPLOYEE_COUNT
------------- ------------------------------ --------------
           50 Shipping                                   45

这有点荒谬* 8-)

答案 2 :(得分:1)

基于Scott的模式:

SQL> with rnk as
  2    (select deptno,
  3            count(*) cnt,
  4            row_number() over (order by count(*) desc) rn
  5     from emp
  6     group by deptno
  7    )
  8  select d.deptno, d.dname, r.cnt
  9  from dept d join rnk r on r.deptno = d.deptno
 10  where r.rn = 1;

    DEPTNO DNAME                 CNT
---------- -------------- ----------
        30 SALES                   6

SQL>

使用源数据

SQL> select deptno, count(*)
  2  from emp
  3  group by deptno;

    DEPTNO   COUNT(*)
---------- ----------
        30          6
        20          3
        10          3

SQL>