Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query: SUM values up to the current record

Basically, having this table:

12.10
 2.35
21.45
35.26

I want to, in each record, calculate the sum of all previous records, like this:

12.10 | 12.10
 2.35 | 14.45
21.45 | 35.90
35.26 | 71.16
like image 384
djeidot Avatar asked May 04 '10 09:05

djeidot


People also ask

How do I get the sum of values in a row in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; If you need to arrange the data into groups, then you can use the GROUP BY clause.

How do you sum a column sum in SQL?

The SQL Server SUM() function is an aggregate function that calculates the sum of all or distinct values in an expression. In this syntax: ALL instructs the SUM() function to return the sum of all values including duplicates. ALL is used by default.

Can we use sum in order by in SQL?

Use ORDER BY if you want to order rows according to a value returned by an aggregate function like SUM() . The ORDER BY operator is followed by the aggregate function (in our example, SUM() ).

What is running sum in SQL?

In SQL, a running total is the cumulative sum of the previous numbers in a column.

How to find sum of all values in a column in SQL?

Sum of all values in a column: 1 For this, we need to use the sum () function. We have to pass the column name as a parameter. 2 This sum () function can be used with the SELECT query for retrieving data from the table. 3 The below example shows to find the sum of all values in a column.

What is the use of SUM () function in SQL?

This sum () function can be used with the SELECT query for retrieving data from the table. The below example shows to find the sum of all values in a column.

What is the difference between count and sum in SQL?

The SQL COUNT(), AVG() and SUM() Functions. The COUNT() function returns the number of rows that matches a specified criterion. The AVG() function returns the average value of a numeric column. The SUM() function returns the total sum of a numeric column. COUNT() Syntax

How to sum current month Records in MySQL?

To sum current month records, use the SUM () and MONTH () function. Let us first create a table −


2 Answers

Assuming that you have two columns, a primary key called id and a column called value then you can use this:

SELECT T1.id, SUM(T2.value)
FROM table1 T1
JOIN table1 T2
ON T2.id <= T1.id
GROUP BY T1.id

If you don't have any unique identifier (why not?) then you can use ROW_NUMBER to create one.

like image 133
Mark Byers Avatar answered Oct 06 '22 10:10

Mark Byers


This is called a running total.

If you have a datetime column you can use something like this:

SELECT t1.id, t1.transactiondatetime, amount,
(
 SELECT SUM(amount) 
 FROM dbo.table1 as t1 
 WHERE t1.transactiondatetime <= t0.transactiondatetime
) AS balance
FROM dbo.table1 AS t0
like image 25
edosoft Avatar answered Oct 06 '22 10:10

edosoft