简体   繁体   English

如何每周增加总数以得到余数? SQL服务器

[英]How to add totals per week to get remainder? SQL Server

I have a table that stores timesheet data of hours worked and for each day, there is an Hours column which has the number of hours worked for that day, and so based on a 5 day work week, on the fifth day, there will be a total from those 5 days in a column next to it called Week Total, so as a sample below for 1 week: 我有一个表,用于存储工作小时数的时间表数据,每天都有一个“小时数”列,其中包含该天的工作小时数,因此基于每周工作5天,在第五天,在它旁边的一列中称为“周总数”的那5天中的总计,因此下面是1周的示例:

    TimeIn                TimeOut                Hours          Week Total
---------------------------------------------------------------------------
2019-02-18 07:10:00     2019-02-18 15:50:00      8.17
2019-02-19 07:20:00     2019-02-19 15:41:00      7.85
2019-02-20 06:57:00     2019-02-20 15:50:00      8.38
2019-02-21 07:12:00     2019-02-21 16:03:00      8.35
2019-02-22 07:09:00     2019-02-22 16:05:00      8.43           41.18  

The "Week Total" column value is nowhere to be found within any table within the database, and so I am needing to write a query that will calculate it for each week and if it totals to over 40 for any week, then take that remainder that is over 40 and add it together with other weeks' remainders. 在数据库内的任何表中都找不到“ Week Total”列的值,因此我需要编写一个查询来计算每周的值,如果任何一周的总数超过40,则取余数超过40,并将其与其他几周的余数相加。

Like for example, in the table shown above, the total is 41.18, and say for another week the total is 42.07. 例如,在上表中,总数为41.18,再说一周,总数为42.07。 Since these weeks are over 40, it will be 1.18 + 2.07 for the total of anything extra that is over 40. So this needs to be calculated for each week which is 5 work days. 由于这几周超过40周,因此超过40周的所有多余部分的总和将为1.18 + 2.07。因此,需要每周计算这5个工作日的时间。

So far I have: 到目前为止,我有:

SELECT
    employeeID,
    SUM(DATEDIFF([second], [TimeIn], [TimeOut]) / 60.0  / 60.0) AS Hours
FROM 
    Time
WHERE 
    TimeIn BETWEEN DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) 
               AND DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), -1)
GROUP BY 
    employeeID

But this is just returning their EmployeeID along with their entire total hours worked within the date ranges I am filtering by. 但这只是返回他们的EmployeeID以及我筛选的日期范围内的全部工作时间。

How would I narrow it down to totals per each week of 5 work days in order to get total hours worked per week and if any are over 40, then take that remainder and add? 我如何将其缩小为每周5个工作日的总计,以便获得每周的总工作时间,如果有超过40个小时,那么将剩余的时间加起来吗?

Does this help? 这有帮助吗?

SELECT
employeeID,
SUM(DATEDIFF([second], [TimeIn], [TimeOut]) / 60.0  / 60.0) AS Hours,
CASE WHEN SUM(DATEDIFF([second], [TimeIn], [TimeOut]) / 60.0  / 60.0) > 40
     THEN SUM(DATEDIFF([second], [TimeIn], [TimeOut]) / 60.0  / 60.0) - 40
     ELSE 0 END OvertimeHours
FROM  Time
WHERE 
    TimeIn BETWEEN DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) 
           AND DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), -1)
GROUP BY 
    employeeID, DATEPART(WEEK,[TimeIn])

Add datepart(weekday, TimeIn) to the group by clause: 将datepart(weekday,TimeIn)添加到group by子句:

select employeeID, sum(HoursReminder)
from(
    SELECT
        employeeID,
        SUM(DATEDIFF([second], [TimeIn], [TimeOut]) / 60.0  / 60.0) - 40.0 AS HoursReminder
    FROM 
        Time
    WHERE 
        TimeIn BETWEEN DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) 
                   AND DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), -1)
    GROUP BY 
        employeeID
        , datepart(weekday, TimeIn)
    ) as fr
group by employeeID

This query will give you total reminder per employee. 该查询将为您提供每位员工的总提醒。

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

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