Table transformer to add a column only if it doesn't exist in source table
Asked by emagnun:

Below are the two tables "actual" and "planned" for the Jira tasks.

![Capture.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/171623i393E653A7161F76E/image-dimensions/372x351?v=v2 "Capture.JPG")

The "actual" table is the result of the Jira query based on the current Jira status as below.

![Capture1.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/171628i622EE071C68FC214/image-dimensions/369x218?v=v2 "Capture1.JPG")

Ideally, when people start working on tasks, there will be more columns like "Count In Progress" and "Count Done". But currently these columns are not there in the output, as you can see.

I want to merge these two tables "actual" and "planned" based on sprint name and then add columns "Count In Progress" and "Count Done" only if they don't exist. How do i use table transformer for this?


Answers:
author: Katerina Kovriga {Stiltsoft}
body: [@emagnun](/t5/user/viewprofilepage/user-id/4359249) ,

Maybe this query will work smoothly for you:

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

SELECT T1.'Sprint / Status' AS 'Sprint',  
COALESCE(T1.'Count TO DO', T2.'TO DO') AS 'TO DO',  
COALESCE(T1.'Count IN PROGRESS', T2.'IN PROGRESS') AS 'IN PROGRESS',  
COALESCE(T1.'Count DONE', T2.'DONE') AS 'DONE'  
FROM T1 LEFT JOIN T2 ON T1.'Sprint / Status' = T2.'Sprint'

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


author: Katerina Kovriga {Stiltsoft}
body: Hi [@emagnun](/t5/user/viewprofilepage/user-id/4359249) ,

You may try to wrap both of your tables (or Table Excerpts) subsequently into the Table Transformer and then into the Table Filter macro.

Then you look up your tables with the help of the Table Transformer macro:

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

SELECT T1.'Sprint / Status' AS 'Sprint name', \*  
FROM T1 LEFT JOIN T2 ON T1.'Sprint / Status' = T2.'Sprint'

The Table Filter macro helps you to hide the duplicate unwanted columns (for example, "Sprint/Status" and "Sprint"):

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

As we used \* in the Table Transformer query, there shouldn't be any errors if you have or don't have certain statuses in the result Pivot table obtained from the Jira Issues macro.


author: Sai velagala
body: Hi [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) , I got the solution already and I too have done the same way . But thanks for replying. Help appreciated ?


author: Katerina Kovriga {Stiltsoft}
body: Hi [@Sai velagala](/t5/user/viewprofilepage/user-id/4778106) ,

The [Table Transformer](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Table+Transformer+macro?utm_source=atl_community) macro is provided by the [Table Filter and Charts for Confluence](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?tab=overview&hosting=server&utm_source=atl_community) app and based on the AlaSQL library (the first link leads to the macro documentation with multiple examples of queries and corresponding use cases).

The query from the stackoverflow works well for your case:

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

SELECT 'Key',  
SUM('num') as 'num'  
FROM (SELECT \* FROM T1 UNION ALL SELECT \* FROM T2)  
GROUP BY 'Key'

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

And there is a more simple way to accomplish the case: wrap the [Pivot Table](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Pivot+Table+macro?utm_source=atl_community) macro on top of the Table Transformer macro (directly for Server/Data Center or via the [Table Toolbox](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Table+Toolbox+macro) macro for Cloud).

Use the standard Merge preset in the Table Transformer macro:

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

And perform the grouping and summing via the Pivot Table macro and more comfortable UI:

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

Hope this helps your case.


author: Katerina Kovriga {Stiltsoft}
body: Hi there,

As this question mentions our [Table Filter and Charts for Confluence](https://marketplace.atlassian.com/apps/27447/table-filter-and-charts-for-confluence?tab=overview&hosting=cloud&utm_source=atl_community) app, we are happy to introduce its new macro -- [Table Spreadsheet](https://docs.stiltsoft.com/display/public/TFAC/How+to+use+Table+Spreadsheet+macro?utm_source=atl_community).

The macro allows you to work with fully functional Excel spreadsheets right in Confluence.

You'll be able to use cells' formulas, filters, conditional formatting, etc., create pivot tables and charts from the page view\&edit mode.

The Table Spreadsheet macro is available for Cloud and Server/Data Center.

![](https://docs.stiltsoft.com/download/attachments/83100938/Conditional%20formatting.png?version=1&modificationDate=1641987630000&api=v2&effects=border-simple,shadow-kn)


author: emagnun
body: You're right. it fails if column does really exist.   

I modified the query as below and looks like this works for both existing/non-existing scenarios. Pls share your thoughts on this.

CASE  
WHEN T1.'Count In Progress' LIKE NULL THEN 'nil'  
ELSE T1.'Count In Progress'  
END


author: Katerina Kovriga {Stiltsoft}
body: Does it work when the first table returns the "In progress" column?

For me the query pastes an empty column but the result is somehow messed up when the first column contains the related column.


author: Katerina Kovriga {Stiltsoft}
body: Then I don't get your use case - what do you suppose to keep in these two columns? And what is supposed to happen when the columns with the same names appear in the table via the Jira Issues macro?

As I understand from your SQL query, you want to somehow create an extra column (or columns according to the set of your statuses) that will be present constantly, but if there is no such column obtained from the Jira Issues macro then you want to pull the column with the same name from the second manually created table full of NULLS in every cell?

I'm not sure of the possible SQL query, never came across the similar case, but you'll get a full table once you'll get the first sprint in the next status.


author: Sai velagala
body: Hi, I have a query regarding it. I am new to SQL on the 'Table transformer' plugin but I am working on this right now and I can across a similar situation. If let's say, from the above two tables, we need to add one of the common column values' by taking another common column as reference. And the key values which are not in both the columns also should be present in the result table. Can u pls give me a query for this which will work in the table transformer plugin?

For Reference, I raised this question in stack overflow too: <https://stackoverflow.com/questions/71270349/adding-values-of-column-of-2-tables-by-using-another-common-column-as-key-in-sql/71271025#71271025>


author: emagnun
body: Finally I got a workaround to add non-existing columns... Not sure if this is the best way but at least it worked.

CASE  
WHEN LENGTH(T1.'Count In Progress') NOT NULL THEN T1.'Count In Progress'  
ELSE ''  
END  
AS 'Count In Progress'


author: emagnun
body: Let me simplify the use case:

Source table gets dynamically created based on Jira query, but the number of columns will not be same always. (Lets say Maximum number of columns in source table is 3, but currently it has 1 column only)

Now, I want to pass this source table to a table-transformer to figure out which column is non-existing and then add that explicitily with all 0 value.


author: emagnun
body: Sorry this doesnt solve my real problem -- **I need "Count In Progress" and "Count Done" to be added to my final table, if the Jira query used in "actual" table doesn't return those columns. In my current scenario Jira query only returns "Count To Do" as you can see in my "actual" table.**

Below is what I tried.

![Capture.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/171680i7AF76E8CEFC5A831/image-dimensions/453x168?v=v2 "Capture.JPG")

Inner table transformer combines both table based on sprint. (**Result is good**)

![Capture1.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/171685i11CC73425D847446/image-dimensions/555x62?v=v2 "Capture1.JPG")

![Capture2.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/171686i24E6C2329C9F8933/image-size/large?v=v2&px=999 "Capture2.JPG")

Outer table transformer checks and add columns "Count In Progress" and "Count Done". **(My SQL seems wrong here .. doesnt work.. pls help me on this)**

![Capture2.JPG](https://community.atlassian.com/t5/image/serverpage/image-id/171684i957DB963D1627BFB/image-size/large?v=v2&px=999 "Capture2.JPG")


Tags: confluence-server

server

table-transformer