Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sum one column and subtract over second column

I want to display the subtraction of two columns. From the first column I need to get sum all value and substract with each value from the second column.

This is the table structure:

id | name | col1  | col2 | date
------------------------------------
432| xxx  | 0     | 15   |2015-11-17
432| yyy  | 10    | 30   |2015-11-19 
432| zzz  | 60    | 40   |2015-11-20  

433| aaa  | 0     | 60   |2015-11-17
433| bbb  | 80    | 20   |2015-11-19 
433| ccc  | 60    | 10   |2015-11-20  

Formula should go:

sum(col1) = 70  =>>>   WHERE ID = 432  

70 - col2            col3
-------------------------
=> 70 - 15         =  55
=> 70 - (30 + 15)  =  25 
=> 70 - (40 + 45)  = -15
---------------------------

sum(col1) = 140  ===>>    WHERE ID = 433  

 140 -   col2         col3
-------------------------
=> 140 -  60        =  80
=> 140 - (60 + 20)  =  60 
=> 140 - (10 + 80)  =  50

result is col3 and Output should be like as

id | name | col1  | col2 | col3 |    date
-------------------------------------------
432| xxx  | 0     | 15   |  55  |  2015-11-17
432| yyy  | 10    | 30   |  25  |  2015-11-19 
432| zzz  | 60    | 40   | -15  |  2015-11-20  

433| aaa  | 0     | 60   |  80  |  2015-11-17
433| bbb  | 80    | 20   |  60  |  2015-11-19 
433| ccc  | 60    | 10   |  50  |  2015-11-20 

EDIT: What if I need the values ​​vary depending on the group as a 432 and 433 id column.

like image 386
Dejan Avatar asked Oct 26 '25 20:10

Dejan


2 Answers

Schema Info

DECLARE @TEST TABLE
  (
     id INT,
     name VARCHAR(10),
     col1       INT,
     col2        int
  ) 

INSERT INTO @TEST VALUES 
(432,'xxx',0, 15 ),
(432,'yyy',10, 30 ),
(432,'zzz',60, 40 ),
(433,'aaa',0, 60 ),
(433,'bbb',80, 20 ),
(433,'ccc',60, 10 )

Query

    SELECT  T.id ,
        T.name ,
        T.col1 ,
        T.col2 ,
        SUM(T.col1) OVER( PARTITION BY T.id ORDER BY T.id ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
        - SUM(T.col2) OVER ( PARTITION BY T.id ORDER BY T.id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS col3
FROM    @TEST T;

Results

id | name | col1  | col2 | col3 |
---------------------------------
432  | xxx  | 0     | 15   |  55  |
432  | yyy  | 10    | 30   |  25  |
432  | zzz  | 60    | 40   | -15  |
433  | aaa  | 0     | 60   |  80  | 
433  | bbb  | 80    | 20   |  60  | 
433  | ccc  | 60    | 10   |  50  | 

SQL Fiddle

like image 173
Krishnraj Rana Avatar answered Oct 28 '25 17:10

Krishnraj Rana


This should work:

declare @total int = (select sum(col1) from Table)
select id, name, col1, col2, @total - (select sum(col2) from Table where date <= T.date) as col3, date from Table T

I was assuming you want to substract every time the previous total (based on the date). I hope this is OK.

like image 38
SmartDev Avatar answered Oct 28 '25 16:10

SmartDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!