Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating a running count & running total across customers with SQL

I have the following table (SQL Server 2012):

DID - cust id
GID - order id
AMT - order amt
Gf_Date - order date
SC - order reversal amount

I'm trying to calculate a running count of orders and a running total of sales by customer so that I can assign a flag to the point in time where a customer achieved cumulative sales of $1,000. As a first step, I've run this query:

Select
  [DID]
, [AMT]
, [Gf_Date]
, COUNT([GID]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [CumlativeTotal]
FROM [dbo].[MCT]
WHERE [SC] is null
ORDER BY [DID]

But I get the error message:

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near 'order'

I posted this earlier with the wrong error message pasted in. Regrets and apologies. What you see above is the result I'm getting. Someone commented that this syntax is incorrect. Now that all is in order, can someone tell me what I'm doing wrong?

Can anyone help me out? Can't find a solution anywhere! Thanks!

like image 300
Chris Howley Avatar asked Jul 26 '13 23:07

Chris Howley


People also ask

How do you calculate a running balance?

The basic running balance would be a formula that adds deposits and subtracts withdrawals from the previous balance using a formula like this: =SUM(D15,-E15,F14). NOTE Why use SUM instead of =D15-E15+F14? Answer: The formula in the first row would lead to a #VALUE!

What is a running count statement?

Returns the running count by row (including the current row) for a set of values. The "<for-option>" defines the scope of the function. The "at" option defines the level of aggregation and can be used only in the context of relational datasources.

What is a running total example?

For example, on 05 Jan 2021, the running total is 66. This is the total number of items sold from 01 Jan 2021 to 05 Jan 2021 (including on 05 Jan 2021). Specifically, the calculation is 10 + 12 + 15 + 9 + 20 = 66. Want to learn about window functions?

How to calculate running count in Excel using countif?

This article shows how to calculate running count by using COUNTIF and COUNTIFS functions. The running totals help you to track number of value occurrences in your data. The trick is to use an expanding range ($E$3:E3) on the COUNTIF function.

How do you calculate running total in Excel?

The running total calculation sums all of the values in a column from the current row the formula is in to the first row in the data set. Therefore, we need to create a range reference that always starts at the first row in the column, down to the current row the formula is in.

How do I find the running total of a department?

[ID] ) AS [Running Total] FROM [department] AS T1 In this SQL Server example, we’ll use the SUM Function and OVER to find the Running Total. In this SQL Server example, we will use PARTITION BY with OVER to find the Running Total.

How to find the running total in SQL Server?

In this SQL Server example, we’ll use the SUM Function and OVER to find the Running Total. In this SQL Server example, we will use PARTITION BY with OVER to find the Running Total.


1 Answers

You should use ROW_NUMBER (link) instead of COUNT:

DECLARE @Threshold NUMERIC(19,2)=1000; -- Use the same data type as `[AMT]`'s data type

Select
  [DID]
, [AMT]
, [Gf_Date]
--, COUNT([GID]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, ROW_NUMBER() OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [RunningGift_Count]
, SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) [CumlativeTotal]
, CASE
      WHEN SUM([AMT]) OVER (PARTITION BY [DID] ORDER BY [Gf_Date]) >= @Threshold THEN 1
      ELSE 0
  END IsThresholdPassed
FROM [dbo].[MCT]
WHERE [SC] is null
ORDER BY [DID]
like image 132
Bogdan Sahlean Avatar answered Sep 27 '22 18:09

Bogdan Sahlean