SlideShare a Scribd company logo
SQL Tips Calculate Running Totals.pptx
How to Calculate Running
Totals in SSMS
One of the most used reasons
you might need to create a
running total is to show a
balance as at the date of the
transaction
Assuming you have a table of sales records
Select SaleDate, Sales_Gross
from [dbo].[SalesSample]
But you need to see the running total of sales at
the end of each date
Using a ‘window function’ we can calculate the
running total from the Sales (Gross) column
Select SaleDate, Sales_Gross
, sum(Sales_Gross) over (order by SaleDate asc) as [Running Total]
from [dbo].[SalesSample]
Breaking this down
Select SaleDate, Sales_Gross
, sum(Sales_Gross) over (order by SaleDate asc) as [Running Total]
from [dbo].[SalesSample]
sum(Sales_Gross)
Defines which method of aggregation to use on which column
over (order by SaleDate asc)
Provides the details of how you want the running total to ‘sort’
as [Running Total]
Defines the name of the new column
You can easily amend this to
create running counts, averages
or other calculations
Hope you found this useful
For more Tips, Tricks and
Timesavers, visit our website
Tips and Timesavers | Select Distinct Limited
Credit: simon.harrison@selectdistinct.co.uk

More Related Content

PPTX
Implicit and Explicit Measures in Power BI.pptx
PPTX
What is Microsoft Fabric - a guide by Select Distinct
PPTX
Euro 2024 Predictions - Quarter Final Results.pptx
PPTX
Euro 2024 Predictions - Round of 16 Results.pptx
PPTX
Euro 2024 Predictions - Group Stage Results.pptx
PPTX
Euro 2024 Predictions - Group Stage Outcomes
PPTX
Year on Year Comparison in Power BI.pptx
PPTX
Sync Slicers in Power BI a step by step guide
Implicit and Explicit Measures in Power BI.pptx
What is Microsoft Fabric - a guide by Select Distinct
Euro 2024 Predictions - Quarter Final Results.pptx
Euro 2024 Predictions - Round of 16 Results.pptx
Euro 2024 Predictions - Group Stage Results.pptx
Euro 2024 Predictions - Group Stage Outcomes
Year on Year Comparison in Power BI.pptx
Sync Slicers in Power BI a step by step guide

More from Select Distinct Limited (20)

PPTX
Using Google Search Console Data in Power BI.pptx
PPTX
Data Lake v Data Warehouse. What is the difference?
PPTX
How to Create Drop Down Lists in Excel, step by step
PPTX
Top 5 SQL Tips and Timesaver 2023, our most popular posts
PPTX
Top 5 Power BI tips 2023 most popular blog posts
PPTX
CTEs in SQL.pptx
PPTX
Calculated Columns and Measures in Power BI.pptx
PPTX
Divide by Zero Errors
PPTX
When to transform data for Power BI.pptx
PPTX
PPTX
Direction of travel on a map in Power BI.pptx
PPTX
PPTX
APPEND data in Power Query
PPTX
Power BI Connect to Google BigQuery.pptx
PPTX
Group by ROLLUP in SQL Server
PPTX
Advanced Top N Measures in Power BI
PPTX
Custom Formats in Power BI
PPTX
Power BI Tips Top N Measures.pptx
PPTX
SQL Tips UNPIVOT Function.pptx
PPTX
Power BI Drill Through
Using Google Search Console Data in Power BI.pptx
Data Lake v Data Warehouse. What is the difference?
How to Create Drop Down Lists in Excel, step by step
Top 5 SQL Tips and Timesaver 2023, our most popular posts
Top 5 Power BI tips 2023 most popular blog posts
CTEs in SQL.pptx
Calculated Columns and Measures in Power BI.pptx
Divide by Zero Errors
When to transform data for Power BI.pptx
Direction of travel on a map in Power BI.pptx
APPEND data in Power Query
Power BI Connect to Google BigQuery.pptx
Group by ROLLUP in SQL Server
Advanced Top N Measures in Power BI
Custom Formats in Power BI
Power BI Tips Top N Measures.pptx
SQL Tips UNPIVOT Function.pptx
Power BI Drill Through
Ad

Recently uploaded (20)

PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
1_Introduction to advance data techniques.pptx
PDF
Lecture1 pattern recognition............
PPTX
SAP 2 completion done . PRESENTATION.pptx
PPT
Quality review (1)_presentation of this 21
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PPTX
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
PPT
ISS -ESG Data flows What is ESG and HowHow
PDF
Mega Projects Data Mega Projects Data
PDF
[EN] Industrial Machine Downtime Prediction
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPT
Miokarditis (Inflamasi pada Otot Jantung)
Acceptance and paychological effects of mandatory extra coach I classes.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Introduction to Knowledge Engineering Part 1
1_Introduction to advance data techniques.pptx
Lecture1 pattern recognition............
SAP 2 completion done . PRESENTATION.pptx
Quality review (1)_presentation of this 21
.pdf is not working space design for the following data for the following dat...
Qualitative Qantitative and Mixed Methods.pptx
Business Ppt On Nestle.pptx huunnnhhgfvu
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
01_intro xxxxxxxxxxfffffffffffaaaaaaaaaaafg
ISS -ESG Data flows What is ESG and HowHow
Mega Projects Data Mega Projects Data
[EN] Industrial Machine Downtime Prediction
oil_refinery_comprehensive_20250804084928 (1).pptx
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Miokarditis (Inflamasi pada Otot Jantung)
Ad

SQL Tips Calculate Running Totals.pptx

  • 2. How to Calculate Running Totals in SSMS
  • 3. One of the most used reasons you might need to create a running total is to show a balance as at the date of the transaction
  • 4. Assuming you have a table of sales records Select SaleDate, Sales_Gross from [dbo].[SalesSample] But you need to see the running total of sales at the end of each date
  • 5. Using a ‘window function’ we can calculate the running total from the Sales (Gross) column Select SaleDate, Sales_Gross , sum(Sales_Gross) over (order by SaleDate asc) as [Running Total] from [dbo].[SalesSample]
  • 6. Breaking this down Select SaleDate, Sales_Gross , sum(Sales_Gross) over (order by SaleDate asc) as [Running Total] from [dbo].[SalesSample] sum(Sales_Gross) Defines which method of aggregation to use on which column over (order by SaleDate asc) Provides the details of how you want the running total to ‘sort’ as [Running Total] Defines the name of the new column
  • 7. You can easily amend this to create running counts, averages or other calculations
  • 8. Hope you found this useful
  • 9. For more Tips, Tricks and Timesavers, visit our website Tips and Timesavers | Select Distinct Limited Credit: [email protected]