Возможный оператор, который возвращает ожидаемые результаты:
Таблица:
CREATE TABLE Persona (
ID int,
Name varchar(50),
[Date] date,
Timer time,
InOrOut varchar(1)
)
INSERT INTO Persona (ID, Name, [Date], Timer, InOrOut)
VALUES
(1, 'Seth', '20200526', '07:00', 'I'),
(2, 'Seth', '20200526', '17:00', 'O'),
(3, 'Maria', '20200525', '06:59', 'I'),
(4, 'Maria', '20200525', '07:00', 'I'),
(5, 'Maria', '20200525', '16:49', 'O'),
(6, 'Maria', '20200525', '17:01', 'O')
Оператор:
SELECT
Name As Name,
Date AS Data,
MIN(case when InOrOut = 'I' then CONVERT(VARCHAR(5), Timer, 108) end) AS [In],
MAX(case when InOrOut = 'O' then CONVERT(VARCHAR(5), Timer, 108) end) AS [Out],
DATEDIFF(hour,
MIN(case when InOrOut = 'I' then Timer end),
MAX(case when InOrOut = 'O' then Timer end)
) AS TotalHours
FROM Persona
GROUP BY [Date], Name
ORDER BY [Date] DESC
Результат:
Name Data In Out TotalHours
Seth 2020-05-26 07:00 17:00 10
Maria 2020-05-25 06:59 17:01 11