Table Transformer Macro - JOIN query
Asked by Jignesh Patel:

I have one table (by Excerpt Include macro) inside a Table Transformer macro with following columns

DeptId, ManagerId, EmployeeId, Salary

I want to show total salary by DeptId and ManagerId  
But  
I want to order the records in descending order of total salary by department

<br />

Select TA.\*  
FROM  
(Select T1.'DeptId', T1.'ManagerId', SUM(T1.'Salary') 'Dept-Manager Salary'  
FROM T1  
GROUP BY T1.'DeptId', T1.'ManagerId') AS TA  
JOIN  
(Select T1.'DeptId', SUM(T1.'Salary') 'Dept Salary'  
FROM T1  
GROUP BY T1.'DeptId') AS TB  
ON TA.'DeptId' = TB.'DeptId'  
ORDER BY  
TB.'Dept Salary' DESC

Why is this not working as expected?


Answers:
author: Katerina Kovriga {Stiltsoft}
body: Hi [@Jignesh Patel](/t5/user/viewprofilepage/user-id/5033002) ,

The JOIN function can be unstable working with several internal SELECTs. We have this improvement on our roadmap, I've linked this question to the ticket to notify you later.

As for now you may use the following structure (don't forget to use the [Table Toolbox](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Table+Toolbox+macro?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro to nest macros if your hosting is Cloud):

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

We just split your original query into three separate queries.

**For TT1:**

Select T1.'DeptId', T1.'ManagerId', SUM(T1.'Salary') 'Dept-Manager Salary'  
FROM T1  
GROUP BY T1.'DeptId', T1.'ManagerId'

**For TT2:**

Select T1.'DeptId', SUM(T1.'Salary') 'Dept Salary'  
FROM T1  
GROUP BY T1.'DeptId'

**For external TT:**

SELECT T1.'DeptId', T2.'Dept Salary', T1.'ManagerId', T1.'Dept-Manager Salary'  
FROM T1 JOIN T\* ON T1.'DeptId' = T\*.'DeptId'  
ORDER BY T2.'Dept Salary' DESC

Then the result table will be as following:

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

Hope this workaround may help.

comments: author: Jignesh Patel
body: Thanks for your quick response, [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746)   
This workaround works for us for now.

Would like to hear when JOIN function is updated as you mentioned.

Thanks a lot, again!


author: Alexey Mikhaylov _Stiltsoft_
body: Hello [@Jignesh Patel](/t5/user/viewprofilepage/user-id/5033002) ,

Let me inform you we have resolved the issue of LEFT/OUTER JOIN functioning incorrectly with the nested SELECT in [Table Transformer](https://docs.stiltsoft.com/tfac/dc-server/how-to-use-table-transformer-macro-42241552.html?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources%20&utm_vendorID=1216111) in version 10.1.0 of [Table Filter and Charts for Confluence](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?tab=overview&hosting=datacenter&utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111).


Tags: aggregate

filter

group

table

table-filter

table-filter-and-charts

transformer