我知道怎么做按小时销售的事情,因为我的计算基于实际销售的时间而不是随着时间的推移而推广。但是,考虑到下面的数据,我试图找出按小时计算劳动力的最佳方法。
EmployeeID InTime OutTime PayRate OTRate DTRate
6 59:09.0 05:17.0 10.75 16.13 21.5
6 33:45.0 54:58.0 10.75 16.13 21.5
6 59:25.0 24:07.0 10.75 16.13 21.5
6 55:18.0 29:35.0 10.75 16.13 21.5
6 59:50.0 02:17.0 10.75 16.13 21.5
7 00:45.0 40:48.0 9.25 13.88 18.5
7 00:21.0 58:41.0 9.25 13.88 18.5
13 00:34.0 27:46.0 9 13.5 18
6 55:23.0 11:02.0 10.75 16.13 21.5
6 37:03.0 30:32.0 10.75 16.13 21.5
我知道我最终会弄明白这一点,但是如果有人能给我一些指示来加快这个过程,并让我做一些过分畏缩的话,我一定会很感激。我需要用这些信息做一些更复杂的报告,但首先我只是寻找最简单的查询,告诉我雇主每小时花在劳动上的费用。随意忽略加班/双倍时间,因为在我得到其他部分后,我可以将其纳入。
编辑:InTime和OutTime字段实际上是DateTimes ...由于某种原因,我在复制/粘贴时转换了值
编辑:结果看起来像......
Hour Labor Cost
1:00 $
2:00 $
3:00 $
4:00 $
5:00 $
etc.
其中$是那小时劳动力的花费。所以我想做的第一件事是创建一个包含24行的表;每小时一个。然后输入/输出时间将加入新表中的小时字段。试着考虑一下现在看起来像什么样的连接表达式......
编辑:马克提醒我,我还需要显示日期和小时,这使我认为我的24行表实际上需要24 *但是我要查询多天。另外我意识到每小时1条记录不能很好地工作,因为人们工作时间很长,所以我认为我需要每分钟做一次记录,然后对这些结果进行分组/总结以获得准确的数据。
答案 0 :(得分:1)
当然没有经过考验,但我相信这应该可以减去我所犯的错误:
-- Assume that there is a tblNumbers table
-- that has been populated with at least the values 0 to 60.
-- First, we need to find the days for which there are time records.
; WITH Days_cte (MyDay) AS
(
SELECT DISTINCT
-- Strip off the time from the date.
DATEADD(DAY, DATEDIFF(DAY, 0, InTime), 0) AS MyDay
FROM tblTimeSheet
UNION
SELECT DISTINCT
-- Strip off the time from the date.
DATEADD(DAY, DATEDIFF(DAY, 0, OutTime), 0) AS MyDay
FROM tblTimeSheet
),
-- Next, explode this into hours AND minutes for every day.
-- This cte will contain 1440 records for every day.
Times_cte (MyTime) AS
(
SELECT
DATEADD(MINUTE, minutes.Number,
DATEADD(HOUR, hours.Number, days.MyDay)) AS MyTime
FROM
Days_cte days JOIN
tblNumbers hours ON hours.Number < 24 JOIN
tblNumbers minutes ON minutes.Number < 60 LEFT JOIN
)
-- Now, determine which of these minutes
-- falls between an employee's in and out time.
SELECT
-- Strip off the minutes, leaving only the hour.
DATEADD(HOUR, DATEDIFF(HOUR, 0, times.MyTime), 0) AS [Hour],
-- Divide by 60, since the aggregation is done by the minute.
SUM(ISNULL(ts.PayRate, 0) / 60) AS LaborCost
FROM
Time_cte times LEFT JOIN
tblTimeSheet ts ON times.MyTime BETWEEN ts.InTime AND ts.OutTime
GROUP BY
DATEADD(HOUR, DATEDIFF(HOUR, 0, times.MyTime), 0)