SQL query to count empty values
Asked by Esteban Borja Lopez:

Hello community,

Im dealing with a very basic case Im not able to make it work, to count the amount of tickets out from a given JIRA filter that has a specific field empty.

I've tried the following expressions to count the total tickets with field "Time Spent" empty vs the non-empty ones (irrespective of the value), so I can calculate the percentage out of those 2, but does not work for this case:

* SUM(IF(T4.'Time Spent' is EMPTY 1, 0)) AS 'Total empty'
* SUM(IF(T4.'Time Spent' is not EMPTY 1, 0)) AS 'Total not-empty'
* SUM(IF(T4.'Time Spent' is EMPTY 1, 0))/(SUM(IF(T14'Time Spent' is EMPTY 1, 0))+SUM(IF(T4.'Time Spent' is not EMPTY 1, 0))) AS 'Fulfilment %'![Screenshot 2022-05-13 at 12.39.50.png](/t5/image/serverpage/image-id/197241i6B43FC2B83EB79F3/image-size/large?v=v2&px=999 "Screenshot 2022-05-13 at 12.39.50.png")

I've tried with with "= EMPTY", "is NULL", "= ''", etc... but get either error or weird results. In the case Im using, for simplicity purposes I have only 1 ticket being dropped by the filter, and the "Time Spent" value s empty, and by using above expression the results I get are

![Screenshot 2022-05-13 at 12.38.24.png](/t5/image/serverpage/image-id/197239i40FA99C802DC73CC/image-size/large?v=v2&px=999 "Screenshot 2022-05-13 at 12.38.24.png")

![Screenshot 2022-05-13 at 12.32.30.png](/t5/image/serverpage/image-id/197236i8CFB29779346DE38/image-size/large?v=v2&px=999 "Screenshot 2022-05-13 at 12.32.30.png")

Expected results:

* Total empty: 1
* Total not-empty: 0
* Fulfilment %: 0

Any hint on what am I doing wrong?

Thanks in advance,

Esteban


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

I've tried to reproduce the issue for the project from our own Jira instance:

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

Here I have ten issues and all of them are with the empty Due Date field (we leave this field blank for this project).

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

SELECT "Empty Total" AS '',  
SUM(IF(T1.'Due' IS NULL, 1, 0)) AS 'Empty Due Dates',  
SUM(IF(T1.'Due' IS NOT NULL, 1, 0)) AS 'Not Empty Due Dates'  
FROM T1

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

The result seems to be satisfactory.

So please try to use the NULL/NOT NULL functions once more. If nothing helps, please raise a [support ticket](https://jsd.stiltsoft.com/servicedesk/customer/portal/2). The portal is confidential, so please provide us with the full SQL query that you are using and the screenshot of your Jira Issues macro in the page view mode (without any macros and with the visible headers). We'll look into the issue closely.

comments: author: Esteban Borja Lopez
body: Hi [@Katerina Kovriga {Stiltsoft}](/t5/user/viewprofilepage/user-id/1464746) , thanks for the reply.

I think there is a problem with SQL or the macro app itself as I've realised that same code sometimes works, and stops working suddenly without no changes on the code (for this case and others scenarios).

I've tried IS NULL NOT and IS NULL expressions with same unexpected results, but this morning, when trying it again, ++**it returned the desired values**++. However, when I extend the SLQ code to fetch/calculate the same but from different tables within the same macro, it gives either error or weird numbers.

Same is happening with other coding for [this case:](https://community.atlassian.com/t5/Jira-Software-questions/Calculate-the-time-a-common-ticket-spent-on-a-given-team-hands/qaq-p/2026970)

\* Using just 1 block of code for 2 tables, it works:

![Screenshot 2022-05-19 at 13.37.50.png](https://community.atlassian.com/t5/image/serverpage/image-id/198123i4301F79FA3A4EB64/image-size/large?v=v2&px=999 "Screenshot 2022-05-19 at 13.37.50.png")

\* But when replicating the code to 4 tables (in same macro), it does not:

![Screenshot 2022-05-19 at 13.37.18.png](https://community.atlassian.com/t5/image/serverpage/image-id/198122i3EDDDD882F5EA61A/image-size/large?v=v2&px=999 "Screenshot 2022-05-19 at 13.37.18.png")

Im checking with with IT team to further open a ticket to Atlassian for review.

In addition, as per the code the "NaN%" should be set as "-" but Im having problems to validate it properly when the value (a result of a division) is not a number, like the case above where it's basically doing 0 / 0 = "NaN" (Not a Number)... is there any "IS NOT INTEGER" or similar to validate it? I've tried several ones and still not make it work.

Thanks in advance and Regards,

Esteban


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

Maybe you should separate the Jira Issues macros and the Table Transformer macros? Leave one source table per macro? The issues may come from the dynamic origin of the source macros (maybe some of the Jira Issues macros are slower to download the information, the Table Transformer macro "sees" the data partly and there is certain inconsistency in the results).

You can also create a [support request](https://jsd.stiltsoft.com/servicedesk/customer/portal/2) - our team will look into the issue closely (it's confidential, you'll be able to share your data and queries freely).

What concerns your second question, I can suggest the following workaround to simply replace your "NaN%", "Infinity" and other errors with the CASE WHEN condition:

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

SELECT 'A', 'B',  
CASE WHEN 'C' LIKE "Infinity"  
THEN "-"  
ELSE 'C'  
END  
AS 'C'  
FROM (SELECT \*, 'A'/'B' AS 'C' FROM T\*)

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


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

indeed, I was about to test that... I will proceed with splitting the macros to make the internal queries/calculations more agile, the same for the automation rules that some are heavy enough to compromise the performance. Will tell you the results once tested.

On regards the proposed workaround, I couldn't make it work adapting my code, still debugging as seems simple enough to fail. What I've tried instead is **LIKE ""**and seems to work nicely:

![Screenshot 2022-05-20 at 10.19.22.png](https://community.atlassian.com/t5/image/serverpage/image-id/198298iF7F200D30623ADDF/image-size/large?v=v2&px=999 "Screenshot 2022-05-20 at 10.19.22.png")

![Screenshot 2022-05-20 at 10.17.44.png](https://community.atlassian.com/t5/image/serverpage/image-id/198297iC7169C761B972F13/image-size/large?v=v2&px=999 "Screenshot 2022-05-20 at 10.17.44.png")

Sometimes try and error drops good results :)


author: Nic Brough -Adaptavist-
body: I suspect trying to use SQL is the problem here - it can't find things that are not there. "Is empty" has nothing to work with.

comments: author: Esteban Borja Lopez
body: Hi [@Nic Brough -Adaptavist-](/t5/user/viewprofilepage/user-id/781454) , thanks for the reply.

Indeed, "is empty" was one of the tries, but the ones working are IS NULL or IS NOT NULL... the problem is in my case it works intermittently as explain above so it make my life a head-ache during debugging the code.

Regards,

Esteban


Tags: cloud

jira-cloud