add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select @@version;
(No column name)
Microsoft SQL Server 2019 (RTM-CU1) (KB4527376) - 15.0.4003.23 (X64)
Dec 6 2019 14:53:33
Copyright (C) 2019 Microsoft Corporation
Express Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor)
Create table TblInOut(Userid int, sStatus Varchar(10), Logtime Varchar(10))
insert into TblInOut Values(1, 'In', '10:10'), (1, 'In', '10:12'), (2, 'In', '10:14'), (3, 'In', '10:16'), (3, 'Out', '10:18'),
(1, 'Out', '10:20'), (4, 'In', '10:22'), (2, 'Out', '10:24')
8 rows affected
Create table Employee (EmployeeId int, Name Varchar(20))
insert into Employee Values(1, 'A'), (2, 'B')
2 rows affected
Select * from(
select Userid,
max(case when sStatus = 'In' then Logtime end) as INTIME,
max(case when sStatus = 'Out' then Logtime end) as OUTIME
from (select t.*,
row_number () over (order by Cast(Logtime as Time)) as seq1,
row_number () over (partition by Userid order by Cast(Logtime as Time)) as seq2
from TblInOut t
) t
group by Userid
)a inner join Employee on a.UserId = Employee.EmployeeId;
Userid INTIME OUTIME EmployeeId Name
1 10:12 10:20 1 A
2 10:14 10:24 2 B
Warning: Null value is eliminated by an aggregate or other SET operation.