sql - 如何避免此查询中出现相同的输出时间?

标签 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                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 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   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

关于sql - 如何避免此查询中出现相同的输出时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48658703/

相关文章:

sql - SQL Server 中从 varchar 到 numeric 的转换

ms-access 查询的 SQL 语法错误

sql - vi编写的Hive文本表

mysql - SQL 连接结果

javascript - 从完整日期获取时的时间错误

datetime - 在 C# 中处理 TimeSpan 异常

c# - Parameters.AddWithValue 不会替换我的值

sql-server - 如何使用Docker和Docker Compose从备份开始自动创建和运行 “clean” SQL Server数据库?

sql-server - 为什么在 Sql Server 中使用嵌套事务

python - 从 Pandas 列中检索唯一元素的最有效方法?