I need to create a report showing the employees that produced on average at least 30 pieces/ day
I have 2 tables:
Employees: id, name, surname
Production: id, date_time, id_employee, [..], quantity_produced
the id from the employees table = id_employee from the production table
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 .
See manual
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.