'How to get final result by subtracting all row values of a single column of a Sql table
I have a amount column in my table.
Amount |
---|
22 |
16 |
4 |
How can I subtract in an ascending order i.e(22-16-4) based on date and get final result : 2
Solution 1:[1]
Here is a working query for SQL Server:
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim Biegeleisen |