如何计算同一列中时间值的工作时间

时间:2013-12-26 16:41:42

标签: sql-server-2008 tsql datetime datediff

我有以下数据,其中所有时钟输入和时钟输出都在同一列中。以下是我注意到数据变得奇怪的领域:

  1. 一个钟表,忘记退出,反之亦然的人
  2. 数据分布很长一段时间(2 - 3年)。
  3. 有相同的时钟输入/时钟输出重复。
  4. 一个人可以使用clockin-Clockout-Break-Clockin-Clockout值。
  5. 一个人可以在下午10点开始工作,并在上午8点结束工作。下一个 早晨。
  6. 我有以下数据供您考虑。另请查看SQL Fiddle以解决此问题。 SQL Fiddle * 编辑:向SQLFiddle添加新数据。 *它只允许8k个字符。伤心。     IF OBJECT_ID('tempdb ..#TempTable')IS NOT NULL DROP TABLE #TempTable     CREATE TABLE #TempTable(         EmployeeName varchar(40),         EmployeeID INT,         PostEditPunchValue DATETIME,         PunchDate DATETIME,         FormatTime varchar(20),     )

    INSERT INTO #TempTable (EmployeeName,EmployeeID, PostEditPunchValue,PunchDate,FormatTime) VALUES 
    ('FNU, LNU M', '12345', '2007-01-01 00:00:00.000', '2007-01-01 00:00:00.000', '12:00 AM' ),
    ('FNU, LNU M', '12345', '2008-01-01 00:00:00.000', '2008-01-01 00:00:00.000', '12:00 AM' ),
    ('FNU, LNU M', '12345', '2007-01-02 15:30:00.000', '2007-01-02 00:00:00.000', ' 3:30 PM' ),
    ('FNU, LNU M', '12345', '2007-01-02 08:17:00.000', '2007-01-02 00:00:00.000', ' 8:17 AM' ),
    ('FNU, LNU M', '12345', '2007-01-02 08:17:00.000', '2007-01-02 00:00:00.000', ' 8:17 AM' ),
    ('FNU, LNU M', '12345', '2009-01-02 08:00:00.000', '2009-01-02 00:00:00.000', ' 8:00 AM' ),
    ('FNU, LNU M', '12345', '2009-01-02 16:25:00.000', '2009-01-02 00:00:00.000', ' 4:25 PM' ),
    ('FNU, LNU M', '12345', '2009-01-02 16:25:00.000', '2009-01-02 00:00:00.000', ' 4:25 PM' ),
    ('FNU, LNU M', '12345', '2007-01-03 00:00:00.000', '2007-01-03 00:00:00.000', '12:00 AM' ),
    ('FNU, LNU M', '12345', '2009-01-03 07:50:00.000', '2009-01-03 00:00:00.000', ' 7:50 AM' ),
    ('FNU, LNU M', '12345', '2009-01-03 07:50:00.000', '2009-01-03 00:00:00.000', ' 7:50 AM' ),
    ('FNU, LNU M', '12345', '2009-01-03 16:01:00.000', '2009-01-03 00:00:00.000', ' 4:01 PM' )
    

    以下是我提出的代码。只要小时不是>它就可以正常工作。 8但是过滤掉一些合法的记录,一个人可能在给定的班次上工作了9-10个小时。有人可以说明什么是错的,我需要做些什么来解决它?

    ;WITH 
    CTE2T AS
    (
        SELECT DISTINCT EmployeeName, PostEditPunchValue from #TempTable
        WHERE PostEditPunchValue BETWEEN PostEditPunchValue AND DATEADD(HH,23,PostEditPunchValue) AND PostEditPunchValue <> PunchDate
        and EmployeeName = 'FNU, LNU M' 
    ),
    CTE1 AS
    (
        SELECT  
             T1.EmployeeName
            ,t1.PostEditPunchValue StartDate
            ,min(t2.PostEditPunchValue) EndDate
        FROM 
            CTE2T AS t1
            LEFT JOIN CTE2T AS t2 ON (t1.EmployeeName = t2.EmployeeName
                                      and  t1.PostEditPunchValue < t2.PostEditPunchValue)
        WHERE 
            t1.EmployeeName = 'FNU, LNU M' 
        group by
            t1.PostEditPunchValue
        ,   t1.EmployeeName
    ),
    CTE2 AS
    ( 
        SELECT 
            EmployeeName, StartDate, EndDate,
            DATEDIFF(HH,StartDate,EndDate) 'Hours' 
        from 
            cte1 
        group by 
            EmployeeName, StartDate, EndDate
    )
    SELECT * FROM cte2 where Hours > 8 or Hours = 0
    

    我得到的输出(来自代码)如下:

    EmployeeName                   StartDate             EndDate        Hours   Flag
    
    
    ANDERSON, LETICIA G|    2007-01-02 08:17:00.000|    2007-01-02 15:30:00.000| 7| 
    ANDERSON, LETICIA G|    2009-01-02 08:00:00.000|    2009-01-02 16:25:00.000| 8| 
    ANDERSON, LETICIA G|    2009-01-03 07:50:00.000|    2009-01-03 16:01:00.000| 9| Y
    

0 个答案:

没有答案