Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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 server 针对这种情况的T-Sql查询-是否需要具有最大员工数的部门ID?_Sql Server_Tsql_Count_Max - Fatal编程技术网

Sql server 针对这种情况的T-Sql查询-是否需要具有最大员工数的部门ID?

Sql server 针对这种情况的T-Sql查询-是否需要具有最大员工数的部门ID?,sql-server,tsql,count,max,Sql Server,Tsql,Count,Max,假设employee表包含员工详细信息和员工的deptId。 要获取每个部门的员工人数 select deptId, COUNT(*) from employee group by deptId; 问题是:要使deptId拥有上述结果集的最大员工数 从员工组中按部门顺序按2描述选择前1个部门,计数(*) (2-参考查询列表中的第二列)-可以。。但是 有没有办法避免订购这台电视机?或者更好的编写sql的方法 谢谢 如果您只需要部门内的MAX员工人数,可以执行以下操作: 没有任何命令,这是很难的

假设employee表包含员工详细信息和员工的deptId。 要获取每个部门的员工人数

select deptId, COUNT(*) from employee group by deptId; 
问题是:要使deptId拥有上述结果集的最大员工数

从员工组中按部门顺序按2描述选择前1个部门,计数(*)

(2-参考查询列表中的第二列)-可以。。但是

有没有办法避免订购这台电视机?或者更好的编写sql的方法

谢谢

如果您只需要部门内的
MAX
员工人数,可以执行以下操作:


没有任何命令,这是很难的,但试试看

   Select deptId, cnt
   From (Select deptId, count(*) cnt
         from employee
         Group By deptId) Z
   Where cnt = (Select Max(cnt) 
                From (Select deptId, count(*) cnt
                      From employee
                      Group By deptId) ZZ) 

你想达到什么目标?为什么定单不好?如果有两个部门的员工人数最多,你期望/想要什么?
   Select deptId, cnt
   From (Select deptId, count(*) cnt
         from employee
         Group By deptId) Z
   Where cnt = (Select Max(cnt) 
                From (Select deptId, count(*) cnt
                      From employee
                      Group By deptId) ZZ)