Joining the total SUM of two tables
Asked by Elena Kondrashova:

Hello Community!

I have two tables:

![Screenshot 2022-11-04 222516.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/224274i99DF2E0B9F8EBA25/image-size/large?v=v2&px=999 "Screenshot 2022-11-04 222516.jpg")

I am using a table filter macro to calculate SUM of hours in each of the columns: Downtime, Overtime and Working Hours.

![Screenshot 2022-11-04 222612.jpg](https://community.atlassian.com/t5/image/serverpage/image-id/224275i6DE4246AB472224C/image-size/large?v=v2&px=999 "Screenshot 2022-11-04 222612.jpg")

I am trying to figure out if there's a way to join these two tables, so I can

a) see the total of Downtime, Overtime and Working Hours per Month in a single table;

b) use a formula \& build a chart that would show what is the percentage of Downtime (60 hrs) out of the Total working hours (70 hrs) for the Month of October.

Thank you for your insights!

Elena


Answers:
author: Luis Machado
body: Hi [@Elena Kondrashova](/t5/user/viewprofilepage/user-id/5056724)

Assuming you're using *Table Filter and Charts* from StillSoft, you can use the table transformer macro to accomplish this: <https://docs.stiltsoft.com/display/TFAC/How+to+use+Table+Transformer+macro>

Use the "lookup Tables" preset and match records by the project column.

comments: author: Elena Kondrashova
body: Hi Luis,

Thank you for your response! Yes, I am trying to use table transformer with table lookup option, but getting a bit stuck.  
The reason: my first table can have multiple rows for October for Project A, while my second table will always have only one row for October for Project A.  
When I try to join these tables, it duplicates the October row from my second table, for every October row I have in the first table. I can't figure out how to avoid this duplication.


author: Luis Machado
body: Hi Elena,

I see the problem. So because your rows have unique data, they will always have their own unique row in the table and the Lookup Table preset will match the duplicates. What you'd have to do is remove the unique values that cause the duplicates.

The Columns responsible for that currently are Date, Overtime, and Downtime.

Overtime and Downtime you could potentially work around by using the SUM() function to add the values from each row in a Month.

Date unfortunately is not something you'd be able to remedy, and it would need to be removed from the dataset to get the end result you want. If Date is required, I don't believe you'll be able to avoid some amount of duplicates.

So the first thing would be to wrap your table with the duplicate entries in a custom Table Transformer with the following SQL:

```
SELECT 'Project' as 'Project', 'Month', SUM(Overtime) as 'Overtime', SUM(Downtime) as 'Downtime'
FROM T1
GROUP BY 'Project', 'Month'
ORDER BY 'Project'
```

Which gives you the following result:

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

Then you nest that macro into another macro on the page that also has the second table in it. The query for the outer macro would look something like this:

```
SELECT *
FROM T1
OUTER JOIN T*
ON T1.'Project' = T*.'Project' AND T1.'Month' = T*.'Month'
```

In the edit view it looks like this:

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

and the end result looks like this:

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

If the dates are necessary, you could potentially have Table 1 live somewhere else and call it into your macro using the Table Excerpt and Table Excerpt Include macros. Then the table could exist with the dates in one view and without them in another so you don't have to duplicate keeping track of the same data.

Finally a word of caution, nesting macros like this can eventually lead to performance impacts depending on how much data you're running your calculations against. The more complex your build the higher the potential, so I would recommend keeping it simple where you can. Hope this helps!


author: Elena Kondrashova
body: Thank you [@Luis Machado](/t5/user/viewprofilepage/user-id/3743773) really appreciate detailed response! I've tried to make suggested changes, and I think it worked. I do feel like the page takes time to load, so I will keep an eye on performance. Once again thanks a lot for your help.


author: Natalie Paramonova [Stiltsoft]
body: Hi [@Elena Kondrashova](/t5/user/viewprofilepage/user-id/5056724) ,

<br />

Thanks to [@Luis Machado](/t5/user/viewprofilepage/user-id/3743773) - you have almost the whole case done!

To wrap everything, I can suggest the following structure:

![Mon 15-1.png](https://community.atlassian.com/t5/image/serverpage/image-id/224927i6BEE2803EF75D647/image-size/large?v=v2&px=999 "Mon 15-1.png")

The query for the internal Table Transformer macro is left as it is.

The query for the external Table Transformer macro is as following:

SELECT \*,  
'Working Hours' - 'Downtime' as 'Not Downtime'  
FROM  
(SELECT \*  
FROM T1  
OUTER JOIN T\*  
ON T1.'Project' = T\*.'Project' AND T1.'Month' = T\*.'Month')

We'll be using the new "Not Downtime" column later for our chart.

Here is the outcome of our macro:

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

The next step is to filter your table by month and count the required totals:

![Mon 15-3.png](https://community.atlassian.com/t5/image/serverpage/image-id/224929i51BF0BA915D96669/image-size/large?v=v2&px=999 "Mon 15-3.png")

The last step is to build your chart:

![Mon 15-4.png](https://community.atlassian.com/t5/image/serverpage/image-id/224930i69111786D6C61CDC/image-size/large?v=v2&px=999 "Mon 15-4.png")

![Mon 15-5.png](https://community.atlassian.com/t5/image/serverpage/image-id/224931iBE39CAE645B2E3EE/image-size/large?v=v2&px=999 "Mon 15-5.png")

Note that if you need to see the whole table with all the totals somewhere on the page, as [@Luis Machado](/t5/user/viewprofilepage/user-id/3743773) has kindly mentioned before, use the [Table Excerpt/Table Excerpt Include](https://docs.stiltsoft.com/display/TFAC/How+to+use+Table+Excerpt+and+Table+Excerpt+Include+macros) macros. For example, place your Table Excerpt between the external Table Transformer and Table Filter macros.

As your hosting is not specified, note that our screenshots were taken for Confluence Server/Data Center. For Cloud you'll need to nest macros via the [Table Toolbox](https://docs.stiltsoft.com/display/TFAC/How+to+use+Table+Toolbox+macro).


Tags: filter

table

table-filter

table-filter-and-charts

table-transformer