Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading