---
title: "SQL Query to Calculate Running Total in SQL Server"  
description: "SQL Query to Calculate Running Total in SQL Server"  
author: "Anubhav Sharma"  
published: 2024-07-15  
updated: 2024-07-16  
canonical: https://www.mindstick.com/forum/160914/sql-query-to-calculate-running-total-in-sql-server  
category: "SQL Server"  
tags: ["database", "sql server", "sql server 2008", "sql server 2012", "sql server 2022"]  
reading_time: 2 minutes  

---

# SQL Query to Calculate Running Total in SQL Server

Hi everyone,

I have a table `Sales` and I need to calculate the [running total](https://www.mindstick.com/forum/33577/calculate-running-total-total-of-a-column-and-row) `SaleAmount` for each day. How can I write this [query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver)?

## Replies

### Reply by Ravi Vishwakarma

To calculate a running total in SQL Server, you can use the `SUM` function along with the `OVER` clause.

Here’s an example query that calculates a running total of the `Amount` column in a table called `Transactions`:

#### Example Table Schema

Assume you have a table named `Transactions` with the following schema:

```plaintext
CREATE TABLE Transactions (
    TransactionID INT PRIMARY KEY,
    TransactionDate DATE,
    Amount DECIMAL(10, 2)
);
```

#### Insert data to Table

```plaintext
INSERT INTO Transactions (TransactionID, TransactionDate, Amount) VALUES
(1, '2023-01-01', 100.00),
(2, '2023-01-02', 150.00),
(3, '2023-01-03', 200.00),
(4, '2023-01-04', 250.00),
(5, '2023-01-05', 300.00),
(6, '2023-01-06', 350.00),
(7, '2023-01-07', 400.00),
(8, '2023-01-08', 450.00),
(9, '2023-01-09', 500.00),
(10, '2023-01-10', 550.00),
(11, '2023-01-11', 600.00),
(12, '2023-01-12', 650.00),
(13, '2023-01-13', 700.00),
(14, '2023-01-14', 750.00),
(15, '2023-01-15', 800.00),
(16, '2023-01-16', 850.00),
(17, '2023-01-17', 900.00),
(18, '2023-01-18', 950.00),
(19, '2023-01-19', 1000.00),
(20, '2023-01-20', 1050.00);
```

#### Sample Query

Here's how you can calculate the running total:

```plaintext
SELECT
    TransactionID,
    TransactionDate,
    Amount,
    SUM(Amount) OVER (ORDER BY TransactionDate) AS RunningTotal
FROM
    Transactions
ORDER BY
    TransactionDate;
```

## Explanation:

- `SELECT TransactionID, TransactionDate, Amount`: Selects the columns to display.
- `SUM(Amount) OVER (ORDER BY TransactionDate) AS RunningTotal`:

   - `SUM(Amount)` calculates the sum of the `Amount` column.
   - `OVER (ORDER BY TransactionDate)` specifies the order in which the rows are processed. It calculates the running total for each row in the order of `TransactionDate`.
   - `AS RunningTotal` names the calculated column as `RunningTotal`.

- `FROM Transactions`: Specifies the table to query.
- `ORDER BY TransactionDate`: Orders the final result set by `TransactionDate`.

This query will produce a result set with each row showing the `TransactionID`, `TransactionDate`, `Amount`, and the `RunningTotal` up to that row, ordered by `TransactionDate`.

## Or

```plaintext
SELECT
    TransactionDate,
    SUM(Amount) AS RunningTotal
FROM Transactions
GROUP BY TransactionDate
```

This query will produce a result set with each row showing the `TransactionDate`, and the `RunningTotal` up to that row.

## Read more

[**Describe Common Table Expressions (CTEs) in SQL server.**](https://www.mindstick.com/forum/160906/describe-common-table-expressions-ctes-in-sql-server)

[**How to Join Multiple Tables and Retrieve Specific Columns in SQL Server?**](https://www.mindstick.com/forum/160909/how-to-join-multiple-tables-and-retrieve-specific-columns-in-sql-server)

[**Implement row-level security in SQL Server to restrict access to data to users.**](https://www.mindstick.com/forum/160896/implement-row-level-security-in-sql-server-to-restrict-access-to-data-to-users)


---

Original Source: https://www.mindstick.com/forum/160914/sql-query-to-calculate-running-total-in-sql-server

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
