Reverse Deltas of an Array


Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

DECLARE @ table(a int, b int identity)

INSERT @ values(5),(9),(1),(3),(8),(7),(8);
--INSERT @ values(1),(2),(3),(4),(5);
--INSERT @ values(18),(19),(17),(20),(16);

WITH c as(SELECT*,rank()over(order by b desc)z FROM @)SELECT g+isnull(sum(-f)over(order
by b),0)FROM(SELECT sum(iif(c.b=1,c.a,0))over()g,d.a-lead(d.a)over(order by d.b)f,c.b
FROM c,c d WHERE c.b=d.z)d

Enter Parameters

Switch sites:
loading Hold tight while we fetch your results