分组之后,我得到这样的结果:
emp_Reader_id DT eventid
----------------------------------------
9999 2018-10-21 08:00:00.000 0
9999 2018-10-22 06:00:00.000 1
插入临时表以获取行号之后
declare @tempProcesstable as table(
[id] [nvarchar](200) NULL,
[time_stamp] datetime NULL,
[AccessType] varchar(3) NULL)
insert into @tempProcesstable
select distinct
t1.emp_Reader_id, t1.DT,t1.eventid
from
daily_attendance_data t2
join
trnevents t1 on t1.emp_reader_id = t2.emp_reader_id
where
(CONVERT(VARCHAR(26), t2.att_Date, 23) >= CONVERT(VARCHAR(26), '2018-10-20', 23)
and CONVERT(VARCHAR(26), t2.att_date, 23) <= CONVERT(VARCHAR(26), '2018-10-21', 23))
and (t1.DT >= t2.in_time and t1.DT <= t2.out_time)
and t1.emp_reader_id = 9999
group by
t1.emp_Reader_id, t1.dt, t1.eventid
order by
t1.emp_reader_id, DT asc
; With CheckIns As
(
Select
Rowemp_reader_id = Row_Number() Over (Partition by id, Cast(time_stamp As Date) Order By time_stamp),
id, time_stamp,
[Date] = Cast(time_stamp As Date),
[Time] = Cast(time_stamp As Time(0))
From
@tempProcesstable
)
select * from checkins
我当前的输出:
Rowemp_reader_id id time_stamp Date Time
---------------------------------------------------
1 9999 2018-10-21 08:00:00.000 2018-10-21 08:00:00
1 9999 2018-10-22 06:00:00.000 2018-10-22 06:00:00
我的预期输出:
Rowemp_reader_id id time_stamp Date Time
---------------------------------------------------------
1 9999 2018-10-21 08:00:00.000 2018-10-21 08:00:00
2 9999 2018-10-22 06:00:00.000 2018-10-22 06:00:00
答案 0 :(得分:0)
尝试使用Row_Number() Over (Partition by id Order By time_stamp)
declare @tempProcesstable as table(
[id] [nvarchar](200) NULL,
[time_stamp] datetime NULL,
[AccessType] varchar(3) NULL)
insert into @tempProcesstable
select distinct t1.emp_Reader_id, t1.DT,t1.eventid from daily_attendance_data t2 join trnevents t1
on t1.emp_reader_id=t2.emp_reader_id where (CONVERT(VARCHAR(26), t2.att_Date, 23) >=CONVERT(VARCHAR(26), '2018-10-20', 23)
and CONVERT(VARCHAR(26), t2.att_date, 23) <=CONVERT(VARCHAR(26), '2018-10-21', 23))
and
(t1.DT >=t2.in_time
and t1.DT <=t2.out_time)
and t1.emp_reader_id=9999
group by t1.emp_Reader_id,t1.dt,t1.eventid order by t1.emp_reader_id,DT asc
; With CheckIns
As (Select Rowemp_reader_id = Row_Number() Over (Partition by id Order By time_stamp),
id, time_stamp,
[Date] = Cast(time_stamp As Date),
[Time] = Cast(time_stamp As Time(0))
From @tempProcesstable)
select * from checkins