Consistent sum of a column in SQL

1

I wanted to calculate the data of a bad table as follows

I have a price column

  

100 140 300 200

I wanted to generate a second column that would make a consecutive sum of the previous table

  

100 240 540 740

I was able to do with Python one more logic I do not know how to do in SQL

    
asked by anonymous 11.02.2018 / 05:49

1 answer

1

To do this, use OVER on the SUM function. OVER will cause SUM to apply under a condition, which in this case will be all numbers up to the current "offset."

SELECT preço, SUM(preço) OVER (ORDER BY preço) AS "SOMA CONSECUTIVA" FROM valores;

See working in SQL Fiddle .

    
11.02.2018 / 13:34