For instance Person A has records for Day 1 as follows
12:01:00 12:07:00 12:30:37 12:57:00
my current solution gives me 12:01:00 (as MIN) and 12:57:00 (as MAX). But i want 12:01:00 (as MIN) and 12:07:00 (as MAX)
if your sample represent your data, try below sql:
with cte as
(
select
ROW_NUMBER() over(order by a.tijdstip) as id
,a.tijdstip
from @temp a
)
select
c.id, c.tijdstip as 'MIN',
c2.tijdstip as 'Max'
from cte c left join cte c2 on c.id = c2.id-1
where (c.id % 2) <> 0
--RESULT
/*
id MIN Max
-------------------- --------------- ---------------
1 12:01:00 12:07:00
3 12:30:37 12:57:00
*/
the ood row id will become the MIN and the even row id will become the MAX