我有这个T-SQL查询:
select *
from
(select
a.ID, u.Name,
cast(a.time as date) as Date,
min(cast(a.time as time)) as Timex,
a.location as Location, a.state as State
from
ATTENDANCE a
join
EMPLOYEE u on a.id = u.id COLLATE DATABASE_DEFAULT
group by
a.id, u.Name, cast(a.time as date), a.location, a.state) x
where
x.Date >= '2019-01-07'
and x.Date <= '2019-01-07'
and x.Location = 'Office'
and x.id = '1'
order by
x.Name asc, x.State asc
结果如下所示:
ID | Name | Date | Timex | Location | State |
---+--------+-----------+--------+----------+-------+
1 |Joe |2019-01-07 |08:00:00| Office | In |
1 |Joe |2019-01-07 |18:00:00| Office | Out |
我该如何从这个结果中获取时间差?因为这个查询每个员工都有两个结果。谢谢。