SQL中的时间范围

时间:2014-07-02 09:37:56

标签: sql sql-server tsql

我正在尝试使用员工BreakIn和BreakOut获取LogIn和LogOut。

select FirstName, LastName,convert(char(10),dtdate,101) as [LOGDATE] 
,min(convert(char(10),dtdate,108)) as TIMEIN
,max(convert(char(10),dtdate,108)) as TIMEOUT
from TransactionLogs


8:00 In
12:00 Break Out
1:00 Break In
5:00 Out


Sample Output:

FirstName LastName In       Break Out   Break In    Out
Jaime      Craig   8:00AM   12:00PM     1:00PM      5:00PM

谢谢! :)

1 个答案:

答案 0 :(得分:0)

DECLARE @TransactionLogs TABLE
(
    FirstName varchar(50),
    LastName varchar(50),
    dtdate DATETIME
)

INSERT INTO @TransactionLogs
( FirstName, LastName, dtdate )
VALUES
( 'Fred', 'Nerfherder', '07/01/2014 08:00:00' ),
( 'Fred', 'Nerfherder', '07/01/2014 12:00:00' ),
( 'Fred', 'Nerfherder', '07/01/2014 13:00:00' ),
( 'Fred', 'Nerfherder', '07/01/2014 18:00:00' ),
( 'Fred', 'Nerfherder', '07/02/2014 07:30:00' ),
( 'Fred', 'Nerfherder', '07/02/2014 12:00:00' ),
( 'Fred', 'Nerfherder', '07/02/2014 13:00:00' ),
( 'Fred', 'Nerfherder', '07/02/2014 17:00:00' ),
( 'Jaime', 'Craig', '07/01/2014 08:00:00' ),
( 'Jaime', 'Craig', '07/01/2014 12:00:00' ),
( 'Jaime', 'Craig', '07/01/2014 13:00:00' ),
( 'Jaime', 'Craig', '07/01/2014 17:00:00' ),
( 'Janet', 'Weiss', '07/01/2014 08:00:00' ),
( 'Janet', 'Weiss', '07/01/2014 17:00:00' ),
( 'Janet', 'Weiss', '07/02/2014 08:00:00' ),
( 'Janet', 'Weiss', '07/02/2014 12:30:00' ),
( 'Janet', 'Weiss', '07/02/2014 14:00:00' ),
( 'Janet', 'Weiss', '07/02/2014 16:30:00' );

SELECT
    t.FirstName,
    t.LastName,
    MAX(CASE WHEN t.Rank = 1 THEN CONVERT(CHAR(10), t.dtdate, 101) END) AS LogDate,
    MAX(CASE WHEN t.Rank = 1 THEN CONVERT(CHAR(10), t.dtdate, 108) END) AS TimeIn,
    MAX(CASE WHEN t.MaxRank = 4 AND t.Rank = 2 THEN CONVERT(CHAR(10), t.dtdate, 108) END) AS BreakOut,
    MAX(CASE WHEN t.MaxRank = 4 AND t.Rank = 3 THEN CONVERT(CHAR(10), t.dtdate, 108) END) AS BreakIn,
    MAX(CASE WHEN (t.MaxRank = 4 AND t.Rank = 4) OR (t.MaxRank = 2 AND t.Rank = 2) THEN CONVERT(CHAR(10), t.dtdate, 108) END) AS TimeOut
FROM
(
    SELECT t.*,
        RANK() OVER (PARTITION BY FirstName, LastName, DATEPART(MONTH, dtdate), DATEPART(DAY, dtdate), DATEPART(YEAR, dtdate) ORDER BY dtdate) as Rank,
        d.MaxRank
    FROM @TransactionLogs t
    OUTER APPLY
    (
        SELECT COUNT(*) AS MaxRank
        FROM @TransactionLogs d
        WHERE d.FirstName = t.FirstName
            AND d.LastName = t.LastName
            AND DATEPART(MONTH, d.dtdate) = DATEPART(MONTH, t.dtdate)
            AND DATEPART(DAY, d.dtdate) = DATEPART(DAY, t.dtdate)
            AND DATEPART(YEAR, d.dtdate) = DATEPART(YEAR, t.dtdate)
    ) d
) t
GROUP BY t.FirstName, t.LastName, DATEPART(MONTH, t.dtdate), DATEPART(DAY, t.dtdate), DATEPART(YEAR, t.dtdate)

一些样本数据和一天的日期分组(如果有人没有休息,我会选择2或4个clockin / clockouts。这是输出:

FirstName   LastName    LogDate     TimeIn    BreakOut  BreakIn   TimeOut
Fred        Nerfherder  07/01/2014  08:00:00  12:00:00  13:00:00  18:00:00  
Fred        Nerfherder  07/02/2014  07:30:00  12:00:00  13:00:00  17:00:00  
Jaime       Craig       07/01/2014  08:00:00  12:00:00  13:00:00  17:00:00  
Janet       Weiss       07/01/2014  08:00:00  NULL      NULL      17:00:00  
Janet       Weiss       07/02/2014  08:00:00  12:30:00  14:00:00  16:30:00