我希望得到员工每周工作的总时数。这是我现在使用的代码:
SELECT
Employee_Number
,CAST([DateTime] as Date) as 'DateTime'
,MIN([DateTime]) as 'MIN'
,MAX([DateTime]) as 'MAX'
,CONVERT(nvarchar, MAX([DateTime]) - MIN([DateTime]), 108) AS [Hours Worked]
FROM tblExtract
GROUP BY
Employee_Number, Cast([DateTime] as Date)
正如您所看到的,由于数据类型--nvarchar,我很难获得每周工作时间的总工作时数。我想使用C#将结果传输到rdlc。
我有两个问题:1)我有什么办法可以获得总金额吗? 2)我应该将其转换为整数还是数字?
答案 0 :(得分:0)
正确的方法是SQL DATEDIFF
函数,带有适当的第一个参数。
要获得一小部分工作小时数,请计算MIN
和MAX
之间的差异,并除以适当的比例。请记住,它将返回一个整数值,截断任何浮点数量。使用分钟或秒来获得分数小时数。
这是1分钟的颗粒:
SELECT
Employee_Number,
CAST([DateTime] AS DATE) AS [DateTime],
MIN([DateTime]) AS [Min],
MAX([DateTime]) AS [Max],
DATEDIFF(n, MIN([DateTime]), MAX([DateTime])) / 60.0 AS [Hours Worked]
FROM tblExtract
GROUP BY
Employee_Number, CAST([DateTime] AS Date)
为了精确到秒级别,请将n
替换为ss
并除以3600.0
。