繁体   English   中英

MYSQL选择多个表+使用数学运算符“平均值/计数”

[英]MYSQL selecting across multiple tables + using mathematical operators Average/Count

我有两个具有以下属性的表:

表:部门

dept_nbr

部门名称

dept_phone

dept_building

dept_mgr

表:员工

emp_nbr

emp_lname

emp_fname

emp_phone

出生日期

emp_date_hired

emp_nbr_of_dependents

emp_dept

dept_nbr = emp_dept

我需要:

每个部门

1.)总编号 员工家属

2.)平均编号 的家属。 -我猜是AVG

3.)总编号 员工-我猜数(*)

有人可以帮我解决这个问题吗?以下是我的代码

  select DEPT_NAME, AVG(EMP_NBR_OF_DEPENDENTs), count(emp_fname) as Total_No_of_Employees from dept,employee where DEPT_NBR = EMP_DEPT group by DEPT_NAME;

第二部分:

包括员工少于50人的部门

select DEPT_NAME, AVG(EMP_NBR_OF_DEPENDENTs), count(emp_fname) as Total_No_of_Employees from dept,employee where DEPT_NBR in (select EMP_DEPT from employee where count(emp_fname)<50) group by DEPT_NAME ;

我尝试了上面的错误1111

谢谢堆

SELECT dept_name,SUM(emp_nbr_of_dependents),AVG(emp_nbr_of_dependents),COUNT(emp_nbr) 
FROM deptartment JOIN employee ON dept_nbr=emp_dept
GROUP BY dept_name
HAVING COUNT(emp_nbr)<50

可以将更多聚合功能组合在一起进行选择。

select DEPT_NAME, 
  sum(EMP_NBR_OF_DEPENDENTs) employee_dependents_sum,
  AVG(EMP_NBR_OF_DEPENDENTs) avg_nbr_dependents, 
  count(emp_nbr) employee_count
from dept,employee 
where DEPT_NBR = EMP_DEPT 
group by DEPT_NAME
having employee_count < 50;

GROUP BY之前应用WHERE部分,然后对结果应用HAVING

http://dev.mysql.com/doc/refman/5.5/en/select.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM