0

我想在 SQL Server 中添加时间。我checktime在数据库中有一个称为datetime数据类型的列。我想从列中添加时间checktime。我怎样才能做到这一点 ??

我正在做这个计算来计算员工在指定日期工作的总小时数。

我的数据库看起来像这样。我想从checktime哪里添加时间checktype = 1checktype = 2然后减去结果。(checktype = 1 表示签入,checktype = 2 表示签出)

我怎样才能做到这一点??

  Id        EmpId              CheckTime                                CheckType
---------------------------------------------------------------------------------
  3           5             2013-01-03 09:00:15.000                         1 
  4           5             2013-01-03 11:00:00.000                         2 
  5           5             2013-01-03 11:30:00.000                         1 
  6           5             2013-01-03 13:00:00.000                         2 
  7           5             2013-01-03 13:30:00.000                         1 
  8           5             2013-01-03 16:00:00.000                         2 
  9           5             2013-01-03 16:30:00.000                         1 
 10           5             2013-01-03 18:00:00.000                         2 
4

1 回答 1

1

如果我正确理解您的问题,这就是我要做的:

SELECT
    EmpId,
    CheckTime,
    CheckType,
    ROW_NUMBER() OVER(PARTITION BY EmpId, DATEADD(dd,DATEDIFF(dd,0,cin.CheckTime),0), CheckType ORDER BY CheckTime) AS Seq
INTO
    #PreparedTable
FROM
    SourceTable


SELECT
    cin.EmpId,
    DATEADD(dd,DATEDIFF(dd,0,cin.CheckTime),0) AS CheckDate,
    (SUM(DATEDIFF(ss,cin.CheckTime,cout.CheckTime)) / 3600.0) AS HoursWorked
FROM
    #PreparedTable cin
JOIN
    #PreparedTable cout
    ON  (cin.EmpId = cout.EmpID)
    AND (DATEDIFF(dd,cin.CheckTime,cout.CheckTime) = 0)
    AND (cin.Seq = cout.Seq)
    AND (cin.CheckType = 1)
    AND (cout.CheckType = 2)
于 2013-01-04T06:19:19.740 回答