⑴ 查询出每个部门的人员总数,sql语句怎么写
sql 使用sum 与 group by
可以统计每个部门的总人数
sum统计总人数 group by根据部门分组
例子
id departmentname number
1 技术 10
2 技术 3
3 销售 50
sql语句
select departmentname ,sum(number)number from table group by departmentname ;
结果
departmentname number
技术 13
销售 50
⑵ Sql 查询所有部门的详细信息和部门人数 查询部门人数不低于5的部门名称和人数
看图,我的测试结果。
⑶ sql里 如何统计一个公司的部门人数 可以有很多子公司 每个子公司可以有相同的部门 每个部门有多个员工
使用GROUP BY分组。
假设表的信息为:Employee(CompName,DeptName,EmpName)
--创建数据表
CREATETABLEEmployee
(
CompName VARCHAR(20),--子公司名称
DeptName VARCHAR(20),--部门名称
EmpName VARCHAR(20)--员工姓名
)
--插入测试数据
INSERTINTOEmployeeVALUES('北京公司','人事部','张')
INSERTINTOEmployeeVALUES('北京公司','财务部','赵')
INSERTINTOEmployeeVALUES('北京公司','人事部','孙')
INSERTINTOEmployeeVALUES('上海公司','人事部','王')
INSERTINTOEmployeeVALUES('上海公司','财务部','李')
--SQL查询
SELECT CompName,DeptName,Number=COUNT(*)
FROM Employee
GROUPBY CompName,DeptName
测试结果:
⑷ sql语句统计各部门不同人员类别的人数
第一步,依据你上边给的语句创建一个视图
createviewv_dept
as
selectbd_deptdoc.deptcodeasdeptcode,
bd_deptdoc.deptlevelasdeptlevel,
bd_deptdoc.deptnameasdeptname,
bd_psndoc.psnnameaspsnname,
bd_psncl.psnclassnameaspsnclassname,
bd_psncl.psnclasscodeaspsnclasscode
frombd_psndoc
innerjoinbd_deptdoc
onbd_psndoc.pk_deptdoc=bd_deptdoc.pk_deptdoc
innerjoinbd_psncl
onbd_psndoc.pk_psncl=bd_psncl.pk_psncl
第二步,动态执行sql,由于你人员类别可能不止就3种,所以要动态执行
declare@sqlvarchar(4000)
set@sql='selectdeptcode,deptname'
select@sql=@sql+',sum(isnull(case[psnclassname]when'''+[psnclassname]+'''then1end,0))as
['+[psnclassname]+']'
from(selectdistinct[psnclassname]fromv_dept)asa
select@sql=@sql+'fromv_deptgroupbydeptcode,deptname'
exec(@sql)
⑸ 查询一个表里 每个部门总人数、以及男生所占的百分比 该怎么写sql语句
select 性别字段 ,count(*) 人数,
cast( convert (decimal(18,2),100*cast(count(*) as float)/cast((select count(*) from 表) as float) ) as varchar)+'%'
from 表 group by 性别字段
--试试,应该可以用
⑹ 用sql语句实现'查询各部门名称和该部门员工数'
具体语句如下:
⑺ 用sql语句查询每个部门中担任相同职务的人数
楼上错了
是group by
order by是按字段排序,group by是按字段分类(分组)
应该是
select 部门,职务,count(*)from 表 group by 部门,职务
如果要结果好看点,再在末尾加个order by 部门
select 部门,职务,count(*)from 表 group by 部门,职务 order by 部门
这样最后显示的结果就是
部门 职务 个数(同部门不同职务)
同一个部门的会显示在一块,不同的职务,当职务的员工个数。
⑻ 一条sql语句查询出每个部门共有多少人
例如数据表为上图则代码为:SELECT 部门,count(查询表名.姓名) AS 人数 INTO 查询输出表 FROM 查询表名 group by 部门