Reputation: 13
I need help converting the following excel logic below to SQL subquery. Your help is very much appreciated.
Column C
Accumulated Minutes
Excel Formula
Upvotes: 1
Views: 112
Reputation: 2210
How about this:
WITH CTE(RowNumber, Date) AS(
select 1, '2012-01-10 16:22' union
select 2, '2012-01-10 16:24' union
select 3, '2012-01-10 16:25' union
select 4, '2012-01-10 16:26' union
select 5, '2012-01-10 19:27'
)
select b.Date, IIF(DATEDIFF(minute, a.Date, b.Date) > 45, 45, DATEDIFF(minute, a.Date, b.Date)) as AccumulatedMinutes
from CTE b left outer join CTE a on a.RowNumber = b.RowNumber - 1
It returns:
2012-01-10 16:22 NULL
2012-01-10 16:24 2
2012-01-10 16:25 1
2012-01-10 16:26 1
2012-01-10 19:27 45
Upvotes: 1