简体   繁体   English

将不同表上的列求和,然后乘以另一个表上的列的值

[英]Sum columns on different tables and multiply by value of a column on another table

I need to compute employees' monthly salaries based on meetings attended, deductions and bonuses given; 我需要根据出席的会议,扣除额和奖金来计算员工的月薪; Employees have different pay per meeting based on their job position. 员工根据工作职位在每次会议上有不同的薪水。

The solution is: salary = (Pay_per_minute * meetings_attended) + bonuses - deductions ; 解决方案是:薪水=(Pay_per_minute *会议参加)+奖金-扣除额;

I have four tables: 我有四个表:

Jobs: Id, title, pay_per_meeting 职位: ID,职务,pay_per_meeting

Employees: Id, Name, job_id 员工: ID,姓名,job_id

Bonuses: Id, amount, employee_id, date 奖金: ID,金额,employee_id,日期

Deductions: Id, amount, employee_id, date 扣除额: ID,金额,employee_id,日期

Meetings: Id, employee_id, date 会议: ID,employee_id,日期

SELECT
COUNT(meetings.employee_id) as meetings_attended, 
COUNT(deductions.amount) as debt, 
COUNT(bonuses.amount) bonus, 
(SELECT jobs.pay_per_attendance from jobs where jobs.id = (select job_id from employees where id=meetings.employee_id)) as pay,
((meetings_attended * pay) + bonus - debt) as salary 
FROM meetings 
JOIN deductions ON deductions.employee_id = meetings.employee_id 
JOIN bonuses ON bonuses.employee_id = meetings.employee_id 
WHERE meetings.employee_id = 1 
GROUP BY MONTH(meetings.date), MONTH(deductions.date), MONTH(bonuses.date)

The above query returns many incorrect values whenever i remove the salary line but gives error of unknown column pay, meetings_attended, debt and bonus, am sure something is wrong with the grouping but i can't just see it. 每当我删除薪水行时,以上查询都会返回许多不正确的值,但是给出了未知的列薪,meetings_attended,债务和奖金的错误,请确保分组有问题,但是我看不到它。

You can't refer to column aliases in the same select list as they're defined, you need to refer to the underlying column. 您不能在定义的相同选择列表中引用列别名,您需要引用基础列。 And a subquery can't access an aggregate calculated in the main query. 子查询无法访问在主查询中计算的聚合。 You need to repeat the aggregate expression, or move everything into a subquery and do the calculation with it in an outer query. 您需要重复聚合表达式,或将所有内容移动到子查询中并在外部查询中使用该表达式进行计算。

Also, all your COUNT() expressions are going to return the same thing, since they're just counting rows (I assume none of the values can be NULL ). 另外,您的所有COUNT()表达式都将返回相同的结果,因为它们只是对行进行计数(我假设所有值都不能为NULL )。 You probably want COUNT(DISTINCT <column>) to get different counts, and you need to use a column that's unique, so they should be the primary key column, eg COUNT(DISTINCT deductions.id) . 您可能希望COUNT(DISTINCT <column>)获得不同的计数,并且需要使用唯一的列,因此它们应该是主键列,例如COUNT(DISTINCT deductions.id)

Another problem is that when you try to sum and count values when you have multiple joins, you end up with a result that's too high, because rows get duplicated in the cross product of all the tables. 另一个问题是,当您尝试对多个连接进行求和和计数时,最终结果会过高,因为所有表的叉积中行都重复。 See Join tables with SUM issue in MYSQL . 请参见在MYSQL中使用SUM联接表 The solution is to calculate the sums from each table in subqueries. 解决方案是从子查询中的每个表中计算总和。

SELECT m.month, m.meetings_attended, d.debt, b.bonus, 
    m.meetings_attended * j.pay_per_meeting + b.amount - d.amount AS salary
FROM (
    SELECT MONTH(date) AS month, COUNT(*) AS meetings_attended
    FROM meetings
    WHERE employee_id = 1
    GROUP BY month) AS m
JOIN (
    SELECT MONTH(date) AS month, COUNT(*) AS bonus, SUM(amount) AS amount
    FROM bonuses
    WHERE employee_id = 1
    GROUP BY month) AS b ON m.month = b.month
JOIN (
    SELECT MONTH(date) AS month, COUNT(*) AS debt, SUM(amount) AS amount
    FROM deductions
    WHERE employee_id = 1
    GROUP BY month) AS d ON m.month = d.month
CROSS JOIN employees AS e
JOIN jobs AS j ON j.id = e.job_id
WHERE e.employee_id = 1

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

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