By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
WITH yourTable AS (
SELECT 22 AS val UNION ALL
SELECT 16 UNION ALL
SELECT 4
)
SELECT TOP 1 val - SUM(val) OVER (ORDER BY val ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) sum_val
FROM yourTable
ORDER BY val DESC;
sum_val |
---|
2 |