[英]I need to calculate the average production per employee per month
I need to create a report showing the employees that produced on average at least 30 pieces/ day 我需要创建一个报告,显示平均每天至少生产30件的员工
I have 2 tables: 我有2张桌子:
Employees: id, name, surname
Production: id, date_time, id_employee, [..], quantity_produced
the id from the employees table = id_employee from the production table 员工表中的id =生产表中的id_employee
SELECT name, surname, Avg(quantity_produced), Month(data_ora) Mnth
FROM production inner join employees on employees.id = production.id_employee
GROUP BY name, quantity_produced, Month(date_time);
OR 要么
select name, surname, quantity_produced, avg(quantity_produced) as avgentrypermonth
from (
select month(date_time) as month ,count(1) as quantity
group by month(date_time));
OR 要么
select name, surname, quantity_produced
from production
inner join employees on employees.id = production.id_employee
where avg(quantity_produced) > 30;
I would most likely go for one off the queries below. 我最有可能在以下查询中取消一项。
But hard to suggest something solid without example data and expected results. 但是,如果没有示例数据和预期结果,很难提出可靠的建议。
Query: 查询:
SELECT
Production.id_employee,
AVG(Production.quantity_produced)
FROM
Production
WHERE
Production.date_time BETWEEN
(LAST_DAY(NOW()) + INTERVAL 1 DAY) - INTERVAL 1 MONTH # first day of current month
AND
LAST_DAY(NOW()) # last day of current month
GROUP BY
Production.id_employee
Query when you need the employees record instead: 查询何时需要员工记录:
SELECT
Employees.name,
Employees.surname,
AVG(Production.quantity_produced)
FROM
Production
INNER JOIN
Employees ON Production.id_employee = Employees.id
WHERE
Production.date_time BETWEEN
(LAST_DAY(NOW()) + INTERVAL 1 DAY) - INTERVAL 1 MONTH # first day of current month
AND
LAST_DAY(NOW()) # last day of current month
GROUP BY
Employees.id
Note the query above assumes at least MySQL 5.7.5+ and SQL 1999+ standards optional feature which is called functional dependency . 请注意,以上查询至少假设了MySQL 5.7.5+和SQL 1999+标准的可选功能,即功能依赖 。
Or as co-related subqueries: which should be fine when indexed correctly 或作为相关子查询:正确建立索引后应该可以
SELECT
(SELECT Employees.name FROM Employees.id = Production.id_employee) AS name,
(SELECT Employees.surname FROM Employees.id = Production.id_employee) AS surname,
AVG(Production.quantity_produced)
FROM
Production
WHERE
Production.date_time BETWEEN
(LAST_DAY(NOW()) + INTERVAL 1 DAY) - INTERVAL 1 MONTH # first day of current month
AND LAST_DAY(NOW()) # last day of current month
GROUP BY
Production.id_employee
for filter aggregated result you need having clause 对于过滤器聚合结果,您需要具有子句
SELECT name, surname, Avg(quantity_produced), Month(date_time) Mnth
FROM production
inner join employees on employees.id = production.id_employee
GROUP BY name, surname, Month(date_time)
having Avg(quantity_produced) > 30 ;
and with year too 以及年份
SELECT name, surname, Avg(quantity_produced), Month(date_time) Mnth, year(date_time) year
FROM production
inner join employees on employees.id = production.id_employee
GROUP BY name, surname, Month(date_time), year(date_time) year
having Avg(quantity_produced) > 30 ;
When working with months, you should be sure that you are taking the year into account as well: 在使用几个月时,您应该确保也考虑到了这一年:
SELECT year(p.data_ora) as yyyy, month(p.data_ora) as mm,
e.name, e.surname, avg(p.quantity_produced),
FROM production p inner join
employees e
on e.id = p.id_employee
GROUP BY year(p.data_ora), month(p.data_ora,
e.name, e.surname
HAVING avg(p.quantity_produced) >= 30
ORDER BY year(p.data_ora), month(p.data_ora),
e.name, e.surname;
This assumes that production
has the quantity produced per day that the employee works. 假设
production
具有员工每天工作的产量。
Notes: 笔记:
I think this should work , can you please try it once. And may be little modification required, Comment here I will try to do the fixes.
1.Select
E.id "Employee ID",
E.name "Employee Name",
Month(P.date_time) "Month",
AVG(P.quantity_produced) "Quantity Produced"
From Employee as E
Join Production as P on E.id=P. id_employee
Group by
E.id,E.name,Month(P.date_time)
Having Avg(P.quantity_produced) > 30
2.Select
E.name "Employee Name",
Month(P.date_time) "Month",
AVG(P.quantity_produced) "Quantity Produced"
From Employee as E
Join Production as P on E.id=P. id_employee
Group by
E.name,Month(P.date_time)
Having Avg(P.quantity_produced) > 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.