结帐时间的SQL问题

时间:2017-07-24 03:44:26

标签: php jquery mysql sql sql-server

我从这样的冲床上获取数据:

checkinout

userID             checktime                     checktype 

100       2017-07-18 06:53:47.000                    I
100       2017-07-18 06:54:47.000                    I
102       2017-07-18 06:55:47.000                    I
104       2017-07-18 06:57:47.000                    I
100       2017-07-18 16:53:47.000                    O
100       2017-07-18 16:53:47.000                    O
102       2017-07-18 16:57:47.000                    O
104       2017-07-18 16:58:47.000                    O
100       2017-07-18 17:53:47.000                    O

我想要这样:

 userID          checkIN time                      checkouttime        tothours   
    100       2017-07-18 06:53:47.000         2017-07-18 17:53:47.000     8.50
    102       2017-07-18 06:55:47.000         2017-07-18 16:57:47.000     9.30
    104       2017-07-18 06:57:47.000         2017-07-18 16:58:47.000     8.50

我使用了很多查询,但没有任何工作。

这是我的疑问:

 DECLARE @temp TABLE
(
    UserID INT,
    Checktime DATETIME,
    CheckStatus CHAR(1)
)

insert into @temp (UserID, Checktime, CheckStatus) 
  (SELECT BADGENUMBER,CHECKTIME,CHECKTYPE
    FROM CHECKINOUT join USERINFO on CHECKINOUT.USERID = USERINFO.USERID where BADGENUMBER ='5969')

SELECT  UserID,
        CAST(I.CheckTime AS DATE) AS [Date],
        CONVERT(VARCHAR(10), I.CheckTime, 108) AS CheckIn,
        CONVERT(VARCHAR(10), O.CheckTime, 108) AS CheckOut,
        CAST(DATEDIFF(MINUTE,I.checkTime,O.CheckTime)/60.0 AS DECIMAL(18,2)) [Hours]
FROM @temp I
OUTER APPLY (
                SELECT TOP 1    Checktime,
                                CheckStatus
                FROM @temp t
                WHERE       t.UserID = I.UserID
                        AND t.Checktime > I.Checktime
                ORDER BY t.Checktime
            ) O
WHERE I.CheckStatus = 'I'
  AND O.CheckStatus = 'O'.........

这也是:

DECLARE @temp TABLE
(
    UserID INT,
    Checktime DATETIME,
    CheckStatus CHAR(1)
)

insert into @temp (UserID, Checktime, CheckStatus) 
  (SELECT BADGENUMBER,CHECKTIME,CHECKTYPE
    FROM CHECKINOUT join USERINFO on CHECKINOUT.USERID = USERINFO.USERID where BADGENUMBER ='6079' )

SELECT 
      t.UserID
    , [Date] = DATEADD(dd, 0, DATEDIFF(dd, 0, t.CheckIn))
    , CheckIn = CONVERT(VARCHAR(10), t.CheckIn, 108)
    , CheckOut = CONVERT(VARCHAR(10), t.CheckOut, 108)
    , [Hours] = CAST(DATEDIFF(MINUTE, t.CheckIn, t.CheckOut) / 60. AS DECIMAL(10,2))
FROM (
    SELECT 
          t.UserID
        , CheckIn = t.Checktime
        , CheckOut = r.Checktime
        , RowNum = ROW_NUMBER() OVER (PARTITION BY t.UserID, r.Checktime ORDER BY 1/0)
    FROM @temp t
    OUTER APPLY (
        SELECT TOP 1 *
        FROM @temp t2
        WHERE t2.UserID = t.UserID
            AND t2.Checktime > t.Checktime
            AND DATEADD(dd, 0, DATEDIFF(dd, 0, t.Checktime)) = DATEADD(dd, 0, DATEDIFF(dd, 0, t2.Checktime))
            AND t2.CheckStatus = 'O'
        ORDER BY t2.Checktime
    ) r
    WHERE t.CheckStatus = 'I'
) t
WHERE t.RowNum = 1.......

1 个答案:

答案 0 :(得分:0)

我猜你想要在最小签到时间(其中checktype = 1)和最大结账时间(其中checktype = 0)之间找到差异

select userID,
    min_date,
    max_date,
    (max_date - min_date) diff
from (
    select distinct userID,
        (
            select min(checktime) 
            from checkinout t2
            where t1.userID = t2.userID
                and t2.checktype = 1
        ) min_date,
        (
            select max(checktime) 
            from checkinout t3
            where t1.userID = t3.userID
                and t3.checktype = 0
        ) max_date
    from checkinout t1
    )
order by userID