By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
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
SELECT Userid,
MAX(CASE WHEN sStatus = 'IN' THEN Logtime ELSE NULL END) AS IN_TIME,
MAX(CASE WHEN sStatus = 'Out' THEN Logtime ELSE NULL END) OUT_TIME
FROM TblInOut
GROUP BY Userid
Userid | IN_TIME | OUT_TIME |
---|---|---|
1 | 10:12 | 10:20 |
2 | 10:14 | 10:24 |
3 | 10:16 | 10:18 |
4 | 10:22 | null |
Warning: Null value is eliminated by an aggregate or other SET operation.