簡體   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