...
drop table temp_events
CREATE TABLE [dbo].[temp_events](
[entry_date_n_time] [datetime] NOT NULL,
[id] varchar NOT NULL,
[status] varchar NOT NULL,
[trans_date] AS (CONVERT([datetime],CONVERT([varchar],[entry_date_n_time],(111)),0)),
[time_portion] AS (CONVERT([varchar],[entry_date_n_time],(108))),
CONSTRAINT [PK_temp_events] PRIMARY KEY CLUSTERED
(
[entry_date_n_time] ASC,
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
delete from temp_events
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 1 2020 9:00:23:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 1 2020 6:00:21:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 1 2020 6:00:42:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 2 2020 9:05:53:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 2 2020 6:00:50:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 2 2020 6:00:51:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 3 2020 8:59:47:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 3 2020 6:20:10:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 3 2020 6:20:11:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 4 2020 9:05:08:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 4 2020 6:14:43:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 4 2020 6:14:44:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 5 2020 9:06:15:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 5 2020 8:03:21:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 5 2020 8:03:22:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 7 2020 7:57:55:000PM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 8 2020 5:05:02:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 8 2020 5:05:04:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 8 2020 7:57:02:000PM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 9 2020 5:01:12:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 9 2020 5:01:14:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 9 2020 7:57:55:000PM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 10 2020 5:10:35:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 10 2020 5:10:36:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 10 2020 7:55:19:000PM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 11 2020 5:02:26:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 11 2020 5:02:27:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 11 2020 7:58:55:000PM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 12 2020 7:17:50:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 12 2020 7:17:52:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 12 2020 7:57:30:000PM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 13 2020 5:25:52:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 13 2020 5:25:54:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 14 2020 7:46:44:000PM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 15 2020 5:00:19:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 15 2020 5:00:21:000AM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 15 2020 9:14:23:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 15 2020 6:00:22:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 15 2020 6:00:23:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 16 2020 9:12:13:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 16 2020 6:03:05:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 16 2020 6:03:07:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 17 2020 9:04:27:000AM','0000175564','01')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 17 2020 6:00:43:000PM','0000175564','02')
INSERT INTO [temp_events] ([entry_date_n_time],[id],[status])
VALUES('Sep 17 2020 6:00:44:000PM','0000175564','02')
select e.trans_date
, timein = e.time_portion
, timeout = convert(varchar,( select max(e2.entry_date_n_time)
from temp_events e2
where e2.entry_date_n_time
between e.entry_date_n_time+0.01 and e.entry_date_n_time + 0.99
and e2.id = e.id
and e2.status = '02'
),108)
, dateout = (select max(e2.trans_date)
from temp_events e2
where e2.entry_date_n_time between
e.entry_date_n_time +0.01 and e.entry_date_n_time + 0.99
and e2.id = e.id
and e2.status = '02'
)
from temp_events e
where status='01'
/*
select entry_date_n_time,id,status
from temp_events t
where trans_date between '2020/09/14' and '2020/09/15'
sno entry_date_n_time id status
1 2020-09-14 19:46:44.000 0000175564 01
2 2020-09-15 05:00:19.000 0000175564 02
3 2020-09-15 05:00:21.000 0000175564 02 -- I want to pick this line
4 2020-09-15 09:14:23.000 0000175564 01
5 2020-09-15 18:00:22.000 0000175564 02
6 2020-09-15 18:00:23.000 0000175564 02 -- my query picking this line
*/
...