Solved Running Total from Query (1 Viewer)

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
I'm trying to calculate running total in a simple query, which the field that will be calculated as running total is come from the multiplication of two fields from a table.
In short, this multiplication field will be in the same query with the running total field, let us said two fields as multiplied from a table, it is named QUANTITY and UNITPRICE to result a field named TOTAL (so here TOTAL = QUANTITY * UNITPRICE), then this TOTAL field will be calculated its running total in the next field of the same query. Please kindly peruse my attachment for the dummy data accdb file, the query will be sorted by INVOICE DATE (can be changed at any time by either ascending or descending) based on field selection criteria (here OFFICE and PARTY EVENT field were chosen as the selection criteria in the 2 queries respectively).

Any feedback from you guys will be much appreciated, is it possible to calculate the running total without the VBA?
One more question, how to do a report with this running total? Thank you.
 

Attachments

  • RunningTotalFromQuerySampleData.zip
    46.9 KB · Views: 193

plog

Banishment Pending
Local time
Today, 04:05
Joined
May 11, 2011
Messages
11,646
I got the running total query, but you lost me around OFFICE and PARTY values. So let's talk about a generic running total using just ID_Invoice, Invoice_Date, Quantity and Unit_Price.

We need to use ID_Invoice and Invoice_Date because you need a distinct way to order your data. You do not have that with Invoice_Date alone. That means if you were to use just the Invoice_Date for ordering both records on 1/22/2020 would have the running total value of 220 (30*2 + 100*1). So you also need to use ID_Invoice so that they distinctly order.

Running totals are done with a correlated sub-query (https://en.wikipedia.org/wiki/Correlated_subquery). That means the subquery and its outer query are able to "communicate" . The subquery is able to access data in the outer query and use it. This is the SQL for a basic running total on your data:

Code:
SELECT mainQ.Invoice_Date, mainQ.ID_Invoice, mainQ.Quantity, mainQ.Unit_Price,
  (SELECT SUM(Quantity*Unit_Price)
  FROM Table_Invoice 
  WHERE mainQ.Invoice_Date >= Table_Invoice.Invoice_Date AND mainQ.ID_Invoice>=Table_Invoice.ID_Invoice)
  AS RunningTotal
FROM Table_Invoice AS mainQ
ORDER BY mainQ.Invoice_Date, mainQ.ID_Invoice;

Paste that into your database and run it. It will give you a jumping off point.
 

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
Hello June7 and Plog, highly appreciated for your reply; OFFICE and PARTY values are just such event category to organize the product, both using Short Text Data Type
 

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
I got the running total query, but you lost me around OFFICE and PARTY values. So let's talk about a generic running total using just ID_Invoice, Invoice_Date, Quantity and Unit_Price.

We need to use ID_Invoice and Invoice_Date because you need a distinct way to order your data. You do not have that with Invoice_Date alone. That means if you were to use just the Invoice_Date for ordering both records on 1/22/2020 would have the running total value of 220 (30*2 + 100*1). So you also need to use ID_Invoice so that they distinctly order.

Running totals are done with a correlated sub-query (https://en.wikipedia.org/wiki/Correlated_subquery). That means the subquery and its outer query are able to "communicate" . The subquery is able to access data in the outer query and use it. This is the SQL for a basic running total on your data:

Code:
SELECT mainQ.Invoice_Date, mainQ.ID_Invoice, mainQ.Quantity, mainQ.Unit_Price,
  (SELECT SUM(Quantity*Unit_Price)
  FROM Table_Invoice
  WHERE mainQ.Invoice_Date >= Table_Invoice.Invoice_Date AND mainQ.ID_Invoice>=Table_Invoice.ID_Invoice)
  AS RunningTotal
FROM Table_Invoice AS mainQ
ORDER BY mainQ.Invoice_Date, mainQ.ID_Invoice;

Paste that into your database and run it. It will give you a jumping off point.
Thank you for the SQL code Plog, unfortunately I got syntax error popup message when putting the code (screenshots as attached).
 

Attachments

  • sql_code.jpg
    sql_code.jpg
    36.7 KB · Views: 151
  • sql_syntax_error.jpg
    sql_syntax_error.jpg
    15.3 KB · Views: 154
Last edited:

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
I got the running total query, but you lost me around OFFICE and PARTY values. So let's talk about a generic running total using just ID_Invoice, Invoice_Date, Quantity and Unit_Price.

We need to use ID_Invoice and Invoice_Date because you need a distinct way to order your data. You do not have that with Invoice_Date alone. That means if you were to use just the Invoice_Date for ordering both records on 1/22/2020 would have the running total value of 220 (30*2 + 100*1). So you also need to use ID_Invoice so that they distinctly order.

Running totals are done with a correlated sub-query (https://en.wikipedia.org/wiki/Correlated_subquery). That means the subquery and its outer query are able to "communicate" . The subquery is able to access data in the outer query and use it. This is the SQL for a basic running total on your data:

Code:
SELECT mainQ.Invoice_Date, mainQ.ID_Invoice, mainQ.Quantity, mainQ.Unit_Price,
  (SELECT SUM(Quantity*Unit_Price)
  FROM Table_Invoice
  WHERE mainQ.Invoice_Date >= Table_Invoice.Invoice_Date AND mainQ.ID_Invoice>=Table_Invoice.ID_Invoice)
  AS RunningTotal
FROM Table_Invoice AS mainQ
ORDER BY mainQ.Invoice_Date, mainQ.ID_Invoice;

Paste that into your database and run it. It will give you a jumping off point.

If I add TOTAL as a calculated field in Table_Invoice, will it simplify the case as the total number will be called from the table (not calculated in the same query with running sum field)?
 
Last edited:

plog

Banishment Pending
Local time
Today, 04:05
Joined
May 11, 2011
Messages
11,646
Here's the database I did it in.
 

Attachments

  • RunningQuery.zip
    24.3 KB · Views: 187

plog

Banishment Pending
Local time
Today, 04:05
Joined
May 11, 2011
Messages
11,646
It won't simplify it in any meaningful way. Still gonna need the correlated sub-query.
 

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
Here's the database I did it in.
Thank you Plog, i tried to select based on event inherited from your code, unfortunately still not giving correct runningsum amount (please see the attached files for screenshots and database files).
 

Attachments

  • query_runningsum_office.png
    query_runningsum_office.png
    11.9 KB · Views: 139
  • RunningTotalFromQuerySampleDataPLOGCode.zip
    30.2 KB · Views: 170
  • sql_runningsum_office.png
    sql_runningsum_office.png
    11.8 KB · Views: 129
Last edited:

plog

Banishment Pending
Local time
Today, 04:05
Joined
May 11, 2011
Messages
11,646
For any field you want to limit your running total to, you must add to the SELECT of the main query and the WHERE of the subquery.
 

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
For any field you want to limit your running total to, you must add to the SELECT of the main query and the WHERE of the subquery.

Have included both in the SQL Code with no luck, screenshoots as attached
 

Attachments

  • QUERY EVENT3.jpg
    QUERY EVENT3.jpg
    30.9 KB · Views: 140
  • SQL EVENT3.jpg
    SQL EVENT3.jpg
    28.7 KB · Views: 123

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
Have included both in the SQL Code with no luck, screenshoots as attached

Have tried also the following code but still unsuccessful :
SELECT mainQ.Event, mainQ.Invoice_Date, mainQ.ID_Invoice, mainQ.Quantity, mainQ.Unit_Price, (SELECT SUM(Quantity*Unit_Price)
FROM Table_Invoice
WHERE mainQ.Event>= Table_Invoice.Event AND mainQ.Invoice_Date >= Table_Invoice.Invoice_Date AND mainQ.ID_Invoice>=Table_Invoice.ID_Invoice) AS RunningTotal
FROM Table_Invoice AS mainQ
WHERE (((mainQ.Event)=3))
ORDER BY mainQ.Event, mainQ.Invoice_Date, mainQ.ID_Invoice;
 

onur_can

Active member
Local time
Today, 02:05
Joined
Oct 4, 2015
Messages
180
You must first create a query that takes the sum. You should then include this query in another query that contains all the fields.

SQL:
SELECT Sum([Query Office].RunningTotal) AS ToRunningTotal
FROM [Query Office];

SQL:
SELECT [Query Office].Event, [Query Office].Invoice_Date, [Query Office].ID_Invoice, [Query Office].Quantity, [Query Office].Unit_Price, [Query GeneralTotal].ToRunningTotal
FROM [Query Office], [Query GeneralTotal];
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 17:05
Joined
May 7, 2009
Messages
19,241
I also made some Query (query1).
based on Query1, create a query with your condition.
 

Attachments

  • RunningTotalFromQuerySampleData.zip
    29.1 KB · Views: 165

CJ_London

Super Moderator
Staff member
Local time
Today, 10:05
Joined
Feb 19, 2013
Messages
16,610
based on the data provided in post#1 this works - it doesn't use a subquery so should be faster. Note it uses a non standard join that cannot be viewed in the query builder. If you open the query builder you will lose the join.

SQL:
SELECT T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price] AS Total, Sum([TT].[Quantity]*[TT].[Unit_Price]) AS RunTot, T.Product_Category
FROM Table_Invoice AS T INNER JOIN Table_Invoice AS TT ON (T.ID_Invoice >= TT.ID_Invoice) AND (T.Invoice_Number = TT.Invoice_Number)
GROUP BY T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price], T.Product_Category, T.Event
HAVING (((T.Event)=1))
ORDER BY T.Invoice_Date DESC, T.ID_Invoice;

The join in question is T.ID_Invoice >= TT.ID_Invoice. So just make it in the query, then go to sql view and add the>

Alternative is to use a where clause instead - but that will potentially be slower

SQL:
SELECT T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price] AS Total, Sum([TT].[Quantity]*[TT].[Unit_Price]) AS RunTot, T.Product_Category
FROM Table_Invoice AS T INNER JOIN Table_Invoice AS TT ON T.Invoice_Number = TT.Invoice_Number
WHERE (((TT.ID_Invoice)<=[t].[id_invoice]))
GROUP BY T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price], T.Product_Category, T.Event
HAVING (((T.Event)=1))
ORDER BY T.Invoice_Date DESC, T.ID_Invoice;
 
Last edited:

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
I also made some Query (query1).
based on Query1, create a query with your condition.

Thank you Arnelgp, I have tested with every event, it will fail for any descending sort combination in Invoice_Date and ID_Invoice, but I'm sure it is almost there, kindly see my attached screenshots using PARTY event selected.
 

Attachments

  • DVIEW_ASC_DESC.jpg
    DVIEW_ASC_DESC.jpg
    7.8 KB · Views: 124
  • DVIEW_DESC_ASC.jpg
    DVIEW_DESC_ASC.jpg
    7.1 KB · Views: 109
  • DVIEW_DESC_DESC.jpg
    DVIEW_DESC_DESC.jpg
    7.1 KB · Views: 143
  • RUNTOT_ASC_DESC.jpg
    RUNTOT_ASC_DESC.jpg
    38.3 KB · Views: 118
  • RUNTOT_DESC_ASC.jpg
    RUNTOT_DESC_ASC.jpg
    38.1 KB · Views: 143
  • RUNTOT_DESC_DESC.jpg
    RUNTOT_DESC_DESC.jpg
    37.8 KB · Views: 105
Last edited:

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
You must first create a query that takes the sum. You should then include this query in another query that contains all the fields.

SQL:
SELECT Sum([Query Office].RunningTotal) AS ToRunningTotal
FROM [Query Office];

SQL:
SELECT [Query Office].Event, [Query Office].Invoice_Date, [Query Office].ID_Invoice, [Query Office].Quantity, [Query Office].Unit_Price, [Query GeneralTotal].ToRunningTotal
FROM [Query Office], [Query GeneralTotal];
Thank you for your feedback, how for the ascending/descending sort in Invoice_Date and ID_Invoice, and also event criteria, can just set it in designview or must accomodate it in the sql code?
 

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
based on the data provided in post#1 this works - it doesn't use a subquery so should be faster. Note it uses a non standard join that cannot be viewed in the query builder. If you open the query builder you will lose the join.

SQL:
SELECT T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price] AS Total, Sum([TT].[Quantity]*[TT].[Unit_Price]) AS RunTot, T.Product_Category
FROM Table_Invoice AS T INNER JOIN Table_Invoice AS TT ON (T.ID_Invoice >= TT.ID_Invoice) AND (T.Invoice_Number = TT.Invoice_Number)
GROUP BY T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price], T.Product_Category, T.Event
HAVING (((T.Event)=1))
ORDER BY T.Invoice_Date DESC, T.ID_Invoice;

The join in question is T.ID_Invoice >= TT.ID_Invoice. So just make it in the query, then go to sql view and add the>

Alternative is to use a where clause instead - but that will potentially be slower

SQL:
SELECT T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price] AS Total, Sum([TT].[Quantity]*[TT].[Unit_Price]) AS RunTot, T.Product_Category
FROM Table_Invoice AS T INNER JOIN Table_Invoice AS TT ON T.Invoice_Number = TT.Invoice_Number
WHERE (((TT.ID_Invoice)<=[t].[id_invoice]))
GROUP BY T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price], T.Product_Category, T.Event
HAVING (((T.Event)=1))
ORDER BY T.Invoice_Date DESC, T.ID_Invoice;
I run both of the code and highlighted the query as runningtotal not giving the correct total at the end.
 

Attachments

  • QUERY_EVENT1.jpg
    QUERY_EVENT1.jpg
    39.2 KB · Views: 135

ykurnia1

New member
Local time
Today, 16:05
Joined
Apr 29, 2020
Messages
23
based on the data provided in post#1 this works - it doesn't use a subquery so should be faster. Note it uses a non standard join that cannot be viewed in the query builder. If you open the query builder you will lose the join.

SQL:
SELECT T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price] AS Total, Sum([TT].[Quantity]*[TT].[Unit_Price]) AS RunTot, T.Product_Category
FROM Table_Invoice AS T INNER JOIN Table_Invoice AS TT ON (T.ID_Invoice >= TT.ID_Invoice) AND (T.Invoice_Number = TT.Invoice_Number)
GROUP BY T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price], T.Product_Category, T.Event
HAVING (((T.Event)=1))
ORDER BY T.Invoice_Date DESC, T.ID_Invoice;

The join in question is T.ID_Invoice >= TT.ID_Invoice. So just make it in the query, then go to sql view and add the>

Alternative is to use a where clause instead - but that will potentially be slower

SQL:
SELECT T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price] AS Total, Sum([TT].[Quantity]*[TT].[Unit_Price]) AS RunTot, T.Product_Category
FROM Table_Invoice AS T INNER JOIN Table_Invoice AS TT ON T.Invoice_Number = TT.Invoice_Number
WHERE (((TT.ID_Invoice)<=[t].[id_invoice]))
GROUP BY T.ID_Invoice, T.Invoice_Date, T.Supplier, T.Payment_Term, T.Invoice_Number, T.Product, T.Quantity, T.Unit_Price, t.[Quantity]*t.[Unit_Price], T.Product_Category, T.Event
HAVING (((T.Event)=1))
ORDER BY T.Invoice_Date DESC, T.ID_Invoice;
When I try to run in ascending order it also still fail
 

Attachments

  • QUERY_EVENT1_ASC.jpg
    QUERY_EVENT1_ASC.jpg
    39.8 KB · Views: 141

CJ_London

Super Moderator
Staff member
Local time
Today, 10:05
Joined
Feb 19, 2013
Messages
16,610
OK so the running sum is not based on invoice?

And the other issue is that ID_Invoice is not in the same order as invoice date (last record ID 5 is dated earlier than ID's 1 and 2)
 

Users who are viewing this thread

Top Bottom