Table Transformer: SQL query for counting values
Asked by Esteban Borja Lopez:

Hello community,

im struggling with a basic operation for counting the amount of values on a given column aiming to get the total value, however the result by using COUNT or SUM return weird data I don't understand the logic behind it.

Basically, I want to get the amount of repetitions of value "Compliance" I have for column 'response time sla'. The example below shows the correct number (i.e. "3") but instead of getting the result in a single cell, I got it as many rows the table has (i.e. "9"). This is the SQL im using:

*SELECT*

*(SELECT COUNT(TT3.'response time sla') FROM T3 AS TT3 WHERE TT3.'response time sla' = "Compliance") AS '\[KPI-DBE.3\]',*

*(SELECT COUNT(TT3.'response time sla') FROM T3 AS TT3) AS '\[KPI-DBE.3 TOTAL\]',*

*FROM T3 AS TT3*

![Screenshot 2022-03-14 at 09.25.34.png](https://community.atlassian.com/t5/image/serverpage/image-id/187951iC56325230B884818/image-dimensions/331x176?v=v2 "Screenshot 2022-03-14 at 09.25.34.png")

![Screenshot 2022-03-14 at 09.25.42.png](https://community.atlassian.com/t5/image/serverpage/image-id/187954iD18DCA6E547185C7/image-dimensions/332x176?v=v2 "Screenshot 2022-03-14 at 09.25.42.png")

However, when im adding a new line to the SQL to get the SUM of all values on that column (by using "SUM" as an alternative to "COUNT") I got this:

![Screenshot 2022-03-14 at 09.26.28.png](https://community.atlassian.com/t5/image/serverpage/image-id/187949i87419FCBAE681004/image-dimensions/333x178?v=v2 "Screenshot 2022-03-14 at 09.26.28.png")

![Screenshot 2022-03-14 at 09.26.28.png](https://community.atlassian.com/t5/image/serverpage/image-id/187955iE5CC3D9903E016E5/image-dimensions/331x177?v=v2 "Screenshot 2022-03-14 at 09.26.28.png")

What should be the right sentence to use to get just "3" or "9" for above queries?

*** ** * ** ***

In addition, taking advantage of the thread, let me ask 2 basic questions about SQL:

1. How can I add a comment on the SQL box to define some instructions about the purpose of the query so reader can understand what it does? I was not able to find any reference on the documentation.

2. How can I create an int or chat variable and allocate some calculation so I can use the variable along the SQL instead of copy+pasting all the time whole calculation sentence?

Thanks very much in advance for your support!


Answers:
author: Katerina Kovriga {Stiltsoft}
body: Hi [@Esteban Borja Lopez](/t5/user/viewprofilepage/user-id/589696) ,

I can suggest using the following SQL query for the initial case:

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

SELECT "Number of entries" AS '',  
SUM(IF(T1.'Project' = "Project 1", 1, 0)) AS 'Project 1',  
SUM(IF(T1.'Project' = "Project 2", 1, 0)) AS 'Project 2',  
SUM(IF(T1.'Project' = "Project 3", 1, 0)) AS 'Project 3'  
FROM T1

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

And you may also check [this Community thread](https://community.atlassian.com/t5/Confluence-questions/How-to-create-a-summarize-table-based-on-columns-of-a-table-in/qaq-p/1709713) for more details.

Don't forget that you may use the [Pivot Table](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Pivot+Table+macro?utm_source=atl_community&utm_vendorID=1216111) macro to count the number of entries with a more friendly UI (the unwanted rows can be filtered further).

What concerns the first additional question, the Table Transformer macro is based on the AlaSQL library. So, you may leave comments in the following way:

/\*Comment goes here\*/

You may also try to use plain Confluence text/headings before/under the table and use user-friendly column names ("Number of entries" for my example).

For the second question I can provide you [with a link](https://community.atlassian.com/t5/Confluence-questions/Table-Filter-Macro-using-Variable-as-Input/qaq-p/1733811) where it's described how to use variables inside the Table Transformer macro: here by variables I mean the contents of the cells used for the query. I'm afraid that it is not possible to store and reuse the whole chunks of SQL queries as for now.

comments: author: Kevin Goff
body: Hi [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) ,

For more than a year I have been using the SUM(IF()) approach as you described above to conditionally count rows, but a few weeks ago my SQL query suddenly stopped working. The query no longer returns a number (not the true count, not zero).

Does the SQL query from your March 14, 2022 post still work for you?  
Was there a change in the Table Transformer product that broke this functionality?


author: Stiltsoft support
body: Hi [@Kevin Goff](/t5/user/viewprofilepage/user-id/4887126) ,

Thanks for reaching out! We see that you've created a ticket via our support portal - we'll continue to investigate the issue there.

Seems that the functionality regarding this precise combination is indeed broken at the moment. So, until everything works as it is supposed again, please consider the following workarounds:

**1) Use the Pivot Table macro instead of the Table Transformer macro**

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

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

**2) Change the SQL query to "separate" the SUM and IF functions**

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

SELECT 'Number of entries',  
SUM('Project 1') AS 'Project 1', SUM('Project 2') AS 'Project 2'  
FROM (SELECT "Number of entries" AS '',  
IF(T1.'Project' = "Project 1", 1, 0) AS 'Project 1',  
IF(T1.'Project' = "Project 2", 1, 0) AS 'Project 2'  
FROM T1)

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

Hope it helps.


author: Stiltsoft support
body: Hi [@Kevin Goff](/t5/user/viewprofilepage/user-id/4887126) ,

The fix was released for Confluence Cloud (it may take 1-2 days to reach all the instances).

And we continue to work on Server/Data Center as well.


author: Kevin Goff
body: SUM(IF()) appears to be working correctly in my queries now (Confluence Cloud). Thank you!


author: Stiltsoft support
body: The fix was also rolled out for Server/Data Center (version 9.6.1).


author: KUMIKO.PERCIVAL_EXT
body: Hi,   
I am adding to this post as I have a similar need.   
My team is using a table with the following column where a mention is included in the same cell as a date. Their preference is to not separate the date out into another column as the table is already getting pretty wide.  

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

I am hoping to count the number of times a given user is mentioned across these column entries (ignoring the dates). So, I'm looking for a SUM IF 'Variable Added by \& Date' contains a specific username.

The following SQL query isn't working for me (I realize that this may contain many errors as I am new to SQL). Is there a fix or an alternative approach that I could look into?  

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

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

Thank you!  


author: Stiltsoft support
body: Hi [@KUMIKO.PERCIVAL_EXT](/t5/user/viewprofilepage/user-id/5183129) ,

Try to replace "=" by "LIKE" in your SQL query:

not SUM(IF(T1.'Mention' **=** "%John Smith%", 1, 0)) AS 'John Smith'

but SUM(IF(T1.'Mention' **LIKE** "%John Smith%", 1, 0)) AS 'John Smith'

Then your query should be working fine.


author: KUMIKO.PERCIVAL_EXT
body: Thank you [@Stiltsoft support](/t5/user/viewprofilepage/user-id/5062270), that worked perfectly!


author: Stiltsoft support
body: Also you may try to use the [Table Spreadsheet](https://docs.stiltsoft.com/tfac/dc-server/table-spreadsheet-83100938.html?utm_source=atl_community&utm_medium=answer&utm_campaign=table-filter-traffic-sources&utm_vendorID=1216111) macro - it will do the trick in Excel-like style.


Tags: confluence-server

filter

pivot-table

server

table

table-filter

table-filter-and-charts

table-filter-charts-spreadsheets

table-spreadsheet

table-transformer