Что-то вроде этого должно работать:
;WITH CTE_DIFF AS (
SELECT [TimeStamp], [State],
DATEDIFF ( second ,
[TimeStamp] ,
LEAD([TimeStamp]) OVER (ORDER BY [TimeStamp])) AS time_diff
FROM mytable
), CTE_PERC AS (
SELECT [TimeStamp], [State], time_diff ,
SUM(time_diff) OVER (ORDER BY [TimeStamp]) * 1.0 /
SUM(time_diff) OVER () * 100 AS perc
FROM CTE_DIFF
)
SELECT [TimeStamp], [State],
COALESCE(LAG(perc) OVER (ORDER BY [TimeStamp]), 0) AS PercentageStart,
perc AS PercentageEnd
FROM CTE_PERC
Демо здесь