我需要计算每个员工每月的平均产量

时间:2019-06-25 10:42:15

标签: mysql sql mysql-workbench

我需要创建一个报告,显示平均每天至少生产30件的员工

我有2张桌子:

Employees: id, name, surname
Production: id, date_time, id_employee, [..], quantity_produced

employees表中的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;

4 个答案:

答案 0 :(得分:1)

我很可能会在下面的查询中选择一个。

但是如果没有示例数据和预期结果,很难提出可靠的建议。

查询:

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

查询何时需要员工记录:

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

请注意,以上查询至少假设使用MySQL 5.7.5+和SQL 1999+标准的可选功能,称为functional dependency

请参见manual

或作为关联子查询:正确建立索引应该没问题

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

答案 1 :(得分:0)

对于过滤器聚合结果,您需要有子句

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 ;

还有年份

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 ;

答案 2 :(得分:0)

在工作数月时,还应确保同时考虑了年份:

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;

这假设production拥有该员工每天工作的产量。

注意:

  • 当表引用多个表时,请限定所有列引用。
  • 使用表别名,以便查询更易于编写和阅读。
  • 记住这一年! (当您工作几个月时)

答案 3 :(得分:0)

    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