Sql 如何在此查询中避免相同的超时?

Sql 如何在此查询中避免相同的超时?,sql,sql-server,datetime,window-functions,analytic-functions,Sql,Sql Server,Datetime,Window Functions,Analytic Functions,我的桌子:Trnevents emp_reader_id EVENTID DT 102 0 2018-01-04 15:57:04.000 102 0 2018-01-04 15:58:05.000 102 1 2018-01-04 16:46:19.000 102 0 2018-01-04 18:15:27.000 102

我的桌子:Trnevents

emp_reader_id   EVENTID     DT
102                0    2018-01-04 15:57:04.000
102                0    2018-01-04 15:58:05.000
102                1    2018-01-04 16:46:19.000
102                0    2018-01-04 18:15:27.000
102                1    2018-01-04 18:20:47.000
102                0    2018-01-04 20:02:05.000
102                0    2018-01-04 21:47:29.000
102                1    2018-01-04 22:00:00.000
我使用了这个查询,它工作得很好,但它的输出时间相同

select
       emp_Reader_id, cast(DT as date) [date]
     ,  DT  as       check_in_1
     ,  next_timestamp as check_out_1


from (
      select
            emp_Reader_id, DT, EVENTID, next_timestamp, next_EVENTID
          , dense_rank() over(partition by emp_Reader_id, cast(DT as date) order by DT) in_rank
      from trnevents t1
      outer apply (
          select top(1) t2.DT, t2.EVENTID
          from trnevents t2
          where t1.emp_Reader_id = t2.emp_Reader_id and t1.EVENTID <> t2.EVENTID
          and cast(t1.DT as date) = cast(t2.DT as date)
          and t1.DT < t2.DT
          order by t2.DT
          ) oa (next_timestamp, next_EVENTID)
      where EVENTID = '0'
     ) d
group by emp_Reader_id, cast(DT as date),DT,next_timestamp
order by emp_reader_id
预期产出:

emp_Reader_id   date    check_in_1  check_out_1
         102    2018-01-04  2018-01-04 15:57:04.000      ----
         102    2018-01-04  2018-01-04 15:58:05.000 2018-01-04 16:46:19.000
         102    2018-01-04  2018-01-04 18:15:27.000 2018-01-04 18:20:47.000
         102    2018-01-04  2018-01-04 20:02:05.000      ----
         102    2018-01-04  2018-01-04 21:47:29.000 2018-01-04 22:00:00.000
是否有可能获得高于预期的产出。任何人都可以提供帮助。
提前感谢

此查询适用于SQL 2012或更高版本

样本数据

create table Trnevents (
    emp_reader_id int
    , EVENTID int
    , DT datetime
)

insert into Trnevents
select
        a, b, cast(c as datetime)
    from
        (values 
            (102, 0, '20180104 15:57:04')
            ,(102, 0, '20180104 15:58:05')
            ,(102, 1, '20180104 16:46:19')
            ,(102, 0, '20180104 18:15:27')
            ,(102, 1, '20180104 18:20:47')
            ,(102, 0, '20180104 20:02:05')
            ,(102, 0, '20180104 21:47:29')
            ,(102, 1, '20180104 22:00:00')
        ) t (a, b, c)
查询:

select
    emp_reader_id, cast(max(DT) as date), max(iif(EVENTID = 0, DT, null)), max(iif(EVENTID = 1, DT, null))
from (
    select
        *, grp = sum(iif(EVENTID = 0, 1, 0) ) over (partition by emp_reader_id order by DT)
    from
        Trnevents
) t
group by emp_reader_id, grp
试试这个:

select * from (
    select *,
           case when Lead(eventid) over (order by dt) = 1
                then Lead(dt) over (order by dt) end [CloseTime]
    from Trnevents
) a where EVENTID = 0
注意:它会重新查询SQL Server 2012或更新版本。

您可以使用该函数探测下一行:

WITH cte AS (
    SELECT *, CASE WHEN eventid = 0 THEN
        LEAD(CASE WHEN eventid = 1 THEN dt ELSE NULL END, 1)
        OVER (PARTITION BY emp_reader_id ORDER BY dt)
    END AS checkout_time
    FROM testdata
)
SELECT *
FROM cte
WHERE eventid = 0
结果:

emp_Reader_id   date    check_in_1  check_out_1
     102    2018-01-04  2018-01-04 15:57:04.000 2018-01-04 16:46:19.000
     102    2018-01-04  2018-01-04 15:58:05.000 2018-01-04 16:46:19.000
     102    2018-01-04  2018-01-04 18:15:27.000 2018-01-04 18:20:47.000
     102    2018-01-04  2018-01-04 20:02:05.000 2018-01-04 22:00:00.000
     102    2018-01-04  2018-01-04 21:47:29.000 2018-01-04 22:00:00.000
| emp_reader_id | eventid | dt                  | checkout_time       |
|---------------|---------|---------------------|---------------------|
| 102           | 0       | 2018-01-04 15:57:04 | NULL                |
| 102           | 0       | 2018-01-04 15:58:05 | 2018-01-04 16:46:19 |
| 102           | 0       | 2018-01-04 18:15:27 | 2018-01-04 18:20:47 |
| 102           | 0       | 2018-01-04 20:02:05 | NULL                |
| 102           | 0       | 2018-01-04 21:47:29 | 2018-01-04 22:00:00 |

在表示层修复它。我只在后端@jarlhUse
行号
中工作,在
签出\u 1
字段上有一个分区。我会回答你的问题,只是你的查询很难看(而且很可怕,因为交叉应用)。你能帮我@TimBiegeleisen找到更好的查询商品吗?但是在这个查询中,我从哪里可以得到trnevents表而不直接给出值?有可能得到进出时间之间的总工作时间吗?你需要以什么格式显示?您可以使用
datediff
函数计算微小差异,然后使用
sum()over(按emp\u reader\u id分区)
获得总和