How to calculate the difference between two columns of running totals
Asked by Eikra Shithil:

I want to merge two tables that have a Date column and a running total column. I need to UNION dates columns from bot tables then ORDER BY Date.

Given data:

GROUP BY 'Date'

Date \| Running total \| Running Closed Total

4/4/2022 \| 4 \|

4/5/2022 \| \| 1

4/6/2022 \| 7 \| 3

4/10/2022 \| 8 \|

4/20/2022 \| \| 4

5/10/2022 \| \| 5

Desired output:

Date \| Running total \| Running Closed Total \| Difference

4/4/2022 \| 4 \| \| 4

4/5/2022 \| \| 1 \| 3

4/6/2022 \| 7 \| 3 \| 7

4/10/2022 \| 8 \| \| 15

4/20/2022 \| \| 4 \| 11

5/10/2022 \| \| 5 \| 6

I thought it would be something like this

SUM('Running Totals' - 'Running Closed Totals')


Answers:
author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@Eikra Shithil](/t5/user/viewprofilepage/user-id/5084427)  

As I see, this question is a follow-up to [this Community thread](https://community.atlassian.com/t5/Confluence-questions/How-to-count-number-of-rows-for-date-then-create-cumulative/qaq-p/2197122#M258957). If I've got your case right, you want to calculate smth similar to Created vs Resolved table based on the running totals.  

![Wed 2-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/228331i98308E48070588B2/image-size/large?v=v2&px=999 "Wed 2-1.png")  

Don't forget to adjust your date format inside the [Table Transformer](https://docs.stiltsoft.com/display/TFAC/How+to+use+Table+Transformer+macro?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro:

![Wed 2-2.png](https://community.atlassian.com/t5/image/serverpage/image-id/228332i2DABE747A65B036F/image-size/large?v=v2&px=999 "Wed 2-2.png")

<br />

And use the following SQL query:  
CREATE TABLE TX;  
INSERT INTO TX  
SELECT 'Date',  
SUM('Count') as 'Count',  
SUM(IF('Resolved' = 'Date', 1, 0)) AS 'Count Resolved' FROM (  
SELECT \*, 1 AS 'Count' FROM T1  
UNION All CORRESPONDING  
SELECT \*, T1.'Resolved' AS 'Date', 0 AS 'Count' FROM T1  
WHERE T1.'Resolved' IS NOT NULL AND T1.'Date' != T1.'Resolved')  
GROUP BY 'Date';  
SELECT TX1.'Date',  
SUM(TX2.'Count') as 'Running Total',  
SUM(TX2.'Count Resolved') as 'Running Resolved',  
SUM(TX2.'Count') - SUM(TX2.'Count Resolved') AS 'Difference'  
FROM TX AS TX1  
JOIN TX AS TX2 on TX1.'Date' \>= TX2.'Date'  
GROUP BY TX1.'Date'  
ORDER BY TX1.'Date'

<br />

![Wed 2-3.png](https://community.atlassian.com/t5/image/serverpage/image-id/228333i9F09F1A0A93ADED2/image-size/large?v=v2&px=999 "Wed 2-3.png")

<br />

Hope this helps your case.

comments: author: Eikra Shithil
body: I get this error

TypeError: Cannot use 'in' operator to search for 'length' in 1


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi, if your tags are correct and you are on Server/Data Center, please check the version of the app that you are using.  
The current one is 9.5.2, if your version is an older one please update.


author: Eikra Shithil
body: What other possible solutions are there to this? It does not seem to be working still.


author: Katerina Rudkovskaya [Stiltsoft]
body: Hi [@Eikra Shithil](/t5/user/viewprofilepage/user-id/5084427),  

Let's move to the [support portal](https://jsd.stiltsoft.com/plugins/servlet/customer/portal/nologin?id=2/user/login?destination=portal%2F2%3Ffrom%3Dmacro) then.  

Go to the upper right corner of the page -\> menu ... -\> View Storage Format. Copy the data and attach your page storage to the ticket. We'll be able to recreate exactly your page with the source tables and macro settings.  

If you don't see the option, please ask your Confluence administrator to do it for you.  
If your source tables are not manually created like we considered them in the Community questions but come from the Jira Issues macros, additionally attach screenshots of them in the published state without any other macros. The Jira Issues macros are not recreated through the page storage.  

And please specify the version of the app/Confluence -- it also helps the support team while looking into the issue.


author: Eikra Shithil
body: I had to edit the question with new desired outputs.


Tags: confluence-server

filter

merge

server

sql

table

table-filter

table-filter-and-charts

table-transformer