-1
SELECT DISTINCT
        s.LastName + ',' + s.FirstName as formattedName
       ,t.Date
       ,t.In1
       ,RIGHT (t.In1, 7) AS TineIn1
       ,RIGHT (t.Out1, 7) AS TimeOut1
       ,RIGHT (t.In2, 7) AS TimeIn2
       ,RIGHT (t.Out2, 7) AS TimeOut1
       ,RIGHT (t.In3, 7) AS TimeIn3
       ,RIGHT (t.Out3, 7) AS TimeOut3
       ,SUM (
              (t.In1-t.Out1)
             +(t.In2-t.Out2)
             +(t.In3-t.Out3)
             ) As WorkedHours

FROM   employee e

JOIN StudentID_EmpID SID
    ON e.EmpID = SID.EmpID
JOIN Student s
    ON SID.StudentID = s.StudentID
    AND s.StudentID = :parm_lb_StudenEE.studentID
JOIN Time t
    ON s.StudentID = t.StudentID

WorkedHours 部分在尝试获得总工作时间时不起作用。我收到以下错误消息“SQL Server 的 Microsoft OLEDB 提供程序:操作数数据类型 datetime 对 sum 运算符无效”

我在数据库中的日期格式是 In1 1/1/1900 8:30:00 AM 和 Out1 1/1/1900 12:30:00 PM 等等。这就是我必须做“RIGHT (t.In1, 7) AS TineIn1”的原因,只是为了得到时间作为日期,这对我来说并不重要。

任何帮助将不胜感激。

4

1 回答 1

1

您可以使用 DATEDIFF 函数来获取两个日期之间的差异,并将它们相加:

 (DATEDIFF(second,t.IN1,t.Out1)
 +DATEDIFF(second,t.IN2,t.Out2)
 +DATEDIFF(second,t.IN3,t.Out3))/3600.0 AS WorkedHours

在这种情况下,将总秒数除以 3600.0 以返回带小数部分的小时数。评论中提到了其他输出选项。

如果任何字段是NULL结果,NULL那么您可以将它们包装在ISNULL()

 (ISNULL(DATEDIFF(second,t.IN1,t.Out1),0)
 +ISNULL(DATEDIFF(second,t.IN2,t.Out2),0)
 +ISNULL(DATEDIFF(second,t.IN3,t.Out3),0))/3600.0 AS WorkedHours
于 2013-07-09T15:59:04.313 回答