如何在有条件的情况下进行SUM?
我需要创建一个 SQL 查询来计算为特定客户 (n°108538) 工作的员工的工作时间。我们区分两种类型的计数:整周工作的人(37 点 50 分)和仅在周六和周日工作的人(22 点 50 分)。
为了不惩罚他们,决定给予他们 15 小时的奖励。
为了了解谁只在周末工作,我们在他们的合同中使用了特定类别,即 n°206。其他员工有其他类别(001、250、604...),因此我无法对该类别进行简单的 GROUP BY。
该请求的目的是计算 2008 年 1 月至 2011 年 3 月期间所有员工的工作小时数,同时考虑到仅在周末工作的员工“提供”的小时数。
我想使用 EXISTS 来知道何时添加这些奖励时间,但我没有得到预期的结果。
这是我写的查询:
SELECT employee.name, employee.surname, SUM(timesheet.hours_par_day +
(CASE
WHEN EXISTS (
SELECT *
FROM (SELECT contract.contrat_id
FROM contract, contract_categories
WHERE contract.customer_id = '108538'
AND contract.begin_date BETWEEN '01/01/2008' AND '01/03/2011'
AND contract_categories.contract_id = contract_categories.id_avenant
AND contract_categories.id_category = '206') ctrsd
WHERE ctrsd.contrat_id = contract.contrat_id)
THEN 15
ELSE 0
END
)
) AS hours_worked
FROM contract JOIN employee ON contract.employee_id = employee.employee_id
JOIN timesheet ON contract.contrat_id = timesheet.contrat_id
WHERE contract.customer_id = '108538'
AND contract.begin_date BETWEEN '01/01/2008' AND '01/03/2011'
AND employee .employee_id IN (
SELECT employee_id
FROM contract
WHERE contract.client_id = '108538' AND contract.end_date >= '01/01/2011')
它计算正确......但不包括额外的 15 小时。我想我滥用了 EXISTS,但我不知道我还能使用什么......有人知道吗?
I need to create an SQL query for the calculation of hours of employees who work for a specific customer (n°108538). We differentiate two types of counting: those who worked the whole week (37h50) and those who worked on Saturday and Sunday only (22h50).
In order to don't penalize them, it was decided to offer them a 15 hours bonus.
To know who worked weekends only, we use a specific category in their contract, the n°206. Other employees have other categories (001, 250, 604...) so I can't make a simple GROUP BY on that category.
The purpose of the request is to calculate the number of hours worked by all the employee from January 2008 to March 2011, taking into account the hours "offered" to those who work on weekends only.
I thought using EXISTS to know when to add these bonus hours, but I don't get the desired result.
This is the query I wrote:
SELECT employee.name, employee.surname, SUM(timesheet.hours_par_day +
(CASE
WHEN EXISTS (
SELECT *
FROM (SELECT contract.contrat_id
FROM contract, contract_categories
WHERE contract.customer_id = '108538'
AND contract.begin_date BETWEEN '01/01/2008' AND '01/03/2011'
AND contract_categories.contract_id = contract_categories.id_avenant
AND contract_categories.id_category = '206') ctrsd
WHERE ctrsd.contrat_id = contract.contrat_id)
THEN 15
ELSE 0
END
)
) AS hours_worked
FROM contract JOIN employee ON contract.employee_id = employee.employee_id
JOIN timesheet ON contract.contrat_id = timesheet.contrat_id
WHERE contract.customer_id = '108538'
AND contract.begin_date BETWEEN '01/01/2008' AND '01/03/2011'
AND employee .employee_id IN (
SELECT employee_id
FROM contract
WHERE contract.client_id = '108538' AND contract.end_date >= '01/01/2011')
It calculates correctly ... but does not include the additional 15 hours. I guess I misuse EXISTS, but I don't know what else I can use... does anyone have any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在这种情况下,您不需要 CASE 中的子查询,您可以改为外连接(尽管,我不确定“contract_financial”来自哪里 - 也许缺少一个表?):
I think in this case you don't need the subquery in the CASE, you can outer join instead (although, I'm not sure where "contract_financial" comes from - maybe a table is missing?):