如何从下面的多个进出时间计算总时数?

时间:2019-08-17 09:54:46

标签: sql sql-server

Below is my table sturture

请及时打卡, 第二次出拳时间

如果可能的话,避免在一分钟内重复打孔

我需要连续获取全部时间,超时时间和总时数 像下面任何格式一样。

我在下面的查询中尝试过,但无法获得预期的输出

WITH  Level1
                  AS (
                        SELECT  A.emp_reader_id,

                                               DT
                         ,A.EventCatId
                         ,A.Belongs_to
                         ,ROW_NUMBER() OVER ( PARTITION BY A.Belongs_to,A.emp_reader_id  ORDER BY DT ) AS RowNum 
                       FROM dbo.trnevents A  


                     )
                   , 
                  LEVEL2
                  AS (-- find the last and next event type for each row 
                       SELECT A.emp_reader_id,A.DT , A.EventCatId ,COALESCE(LastVal.EventCatId, 10) AS LastEvent,
                                                                COALESCE(NextVal.EventCatId, 10) AS NextEvent ,A.Belongs_to
                       FROM Level1 A 
                               LEFT JOIN Level1 LastVal 
                                  ON A.emp_reader_id = LastVal.emp_reader_id and  A.Belongs_to=LastVal.Belongs_to
                                     AND A.RowNum - 1 = LastVal.RowNum 
                               LEFT JOIN Level1 NextVal 
                                  ON A.emp_reader_id = NextVal.emp_reader_id and  A.Belongs_to=NextVal.Belongs_to
                                     AND A.RowNum + 1 = NextVal.RowNum 
                     )
                     select * from level2 where emp_reader_id=92 order by dt desc  

预期输出:

sq

2 个答案:

答案 0 :(得分:1)

尝试以下脚本。我将所有带有Sam Minutes的DT视为一次输入。

WITH CTE AS
(
    SELECT MAX(emp_reader_id) emp_reader_id,
    CAST(DT AS DATE) Date_for_Group,
    LEFT(CAST(DT AS VARCHAR),16) Time_For_Group,
    ROW_NUMBER() OVER(PARTITION BY CAST(DT AS DATE) ORDER BY LEFT(CAST(DT AS VARCHAR),16)) RN,
    CASE 
        WHEN ROW_NUMBER() OVER(PARTITION BY CAST(DT AS DATE) ORDER BY LEFT(CAST(DT AS VARCHAR),16))%2 = 0 THEN 'OUT' 
        ELSE 'IN' 
    END In_Out
    FROM your_table
    GROUP BY CAST(DT AS DATE),LEFT(CAST(DT AS VARCHAR),16)
)

SELECT A.emp_reader_id,A.Date_for_Group,
SUM(DATEDIFF(Minute,CAST(A.Time_For_Group AS DATETIME),CAST(B.Time_For_Group AS DATETIME)))/60 Hr,
SUM(DATEDIFF(Minute,CAST(A.Time_For_Group AS DATETIME),CAST(B.Time_For_Group AS DATETIME)))%60 Min
FROM CTE A
INNER JOIN CTE B 
ON A.emp_reader_id = B.emp_reader_id
AND A.RN = B.RN -1
AND A.Date_for_Group = B.Date_for_Group
WHERE A.In_Out  = 'IN'
GROUP BY A.emp_reader_id,A.Date_for_Group

答案 1 :(得分:1)

首先将行号分配给datetime列,然后从行号+1开始相同的结果集 然后将Inner加入行号。之后,从timein和out列中选择一个max最大值,然后按日期分组以获取当天的总工作时间。希望能帮助到你。

 select empid
  ,date
  ,min(timein) as timein,max (timeout) timeout,convert(nvarchar(20),datediff(hh,min (timein),max(timeout))%24) 
  +':'+
  convert(nvarchar(20),datediff(mi,min (timein),max(timeout))%60) as totalhrs
from(
Select a.empid,cast(a.dt as date) date,b.dt as timein,a.dt as timeout from(
SELECT  DT
  ,[empid]   
  , id
  ,row_number() over(order by dt) as inn
FROM [test1].[dbo].[Table_2]
)a
inner join(
SELECT distinct DT
  ,[empid]
  , id
  ,rank() over(order by dt)+1 as out
FROM [test1].[dbo].[Table_2])b
on FORMAT(a.dt,'hh:mm') <> FORMAT(b.dt,'hh:mm') 
and cast(a.dt as date)=cast(b.dt as date) 
and a.inn=b.out)b
group by b.empid,b.date