Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 对计数聚合函数使用最小聚合函数_Sql_Subquery_Aggregate Functions - Fatal编程技术网

Sql 对计数聚合函数使用最小聚合函数

Sql 对计数聚合函数使用最小聚合函数,sql,subquery,aggregate-functions,Sql,Subquery,Aggregate Functions,我使用了这个查询: select D.DeptID, D.Dept, count(E.EmployeeID) as TotalStaff from Employees as E right join Departments as D on D.DeptID = E.DeptID group by D.DeptID, D.Dept; 要返回此文件: DeptID_|_Dept___________|TotalStaff 40 | Marketing | 2 50

我使用了这个查询:

 select D.DeptID, D.Dept, count(E.EmployeeID) as TotalStaff
 from Employees as E
 right join Departments as D
 on D.DeptID = E.DeptID 
 group by D.DeptID, D.Dept;
要返回此文件:

DeptID_|_Dept___________|TotalStaff
40     | Marketing      | 2
50     | Accounting     | 3
60     | Manager        | 3
70     | GeneralStaff   | 1
80     | HumanResources | 1
90     | Production     | 0
100    | Sales          | 0
现在我想列出部门ID、部门以及员工人数最少的部门的员工人数,因此我尝试了以下方法:

SELECT MIN(mycount) 
FROM 
(
 select D.DeptID, count(E.EmployeeID) as mycount
 from Employees as E
 right join Departments as D
 on D.DeptID = E.DeptID 
 group by D.DeptID
);
但是我得到一个错误,它指出:“;”附近的语法不正确。
我所要做的就是返回员工数量最少的部门。请任何人帮我解决这个问题。

您错过了
子查询
别名

所以,您的子查询有这样的别名

SELECT MIN(mycount) 
FROM (select D.DeptID, count(E.EmployeeID) as mycount
      from Employees as E
      right join Departments as D on D.DeptID = E.DeptID 
      group by D.DeptID
     ) s; -- alias missed 

编写此查询的正常方法是使用ANSI标准
rank()
函数:

select d.*
from (select D.DeptID, D.Dept, count(E.EmployeeID) as TotalStaff,
             rank() over (order by count(E.EmployeeID) asc) as seqnum
      from Departments d left join
           Employees E
           on D.DeptID = E.DeptID 
      group by D.DeptID, D.Dept
     ) d
where seqnum = 1;
注意,我还将
连接
切换为
左连接
<代码>左连接通常更容易遵循(至少对于从左到右阅读语言的人来说是这样),因为它表示将所有行保留在第一个表中,而不是最后一个表中。

尝试添加

as tablename

在你最后一次考试之后)和期末考试之前

请尝试以下查询:

对于最小值:

 select min(A.mycount) as min_count
    from  (
         select D.DeptID, count(E.EmployeeID) as mycount
         from Departments as D
         left outer join Employees as E on D.DeptID = E.DeptID 
         group by D.DeptID
    ) as A
select max(A.mycount) as max_count
from  (
     select D.DeptID, count(E.EmployeeID) as mycount
     from Departments as D
     left outer join Employees as E on D.DeptID = E.DeptID 
     group by D.DeptID
) as A
对于最大值:

 select min(A.mycount) as min_count
    from  (
         select D.DeptID, count(E.EmployeeID) as mycount
         from Departments as D
         left outer join Employees as E on D.DeptID = E.DeptID 
         group by D.DeptID
    ) as A
select max(A.mycount) as max_count
from  (
     select D.DeptID, count(E.EmployeeID) as mycount
     from Departments as D
     left outer join Employees as E on D.DeptID = E.DeptID 
     group by D.DeptID
) as A

为子查询使用名称。您使用的是什么数据库?我使用的是为任务创建的数据库。它已被设置为活动数据库@gordonlinoff,您正在使用哪个?“SQL”只是一种查询语言,而不是特定数据库产品的名称。请为您正在使用的数据库产品添加标记,
postgresql
oracle
sqlserver
db2
。。。