Conditional Sum (variable) - jasper-reports

A report in iReport (4.0.1) with various fields includes: $F{value} (Integer) and $F{created_at}.
I'd like to calculate variables that would give:
the sum of $F{value} when $F{created_at} is before a given date
the sum of $F{value} when $F{created_at} is after a given date
Any idea how this could be done?

You will have to use two different variables to do this. For your variables, use something like this in the 'Variable Expression'. The Date class also has an after() function. If the expression evaluates to true $F{value} will be added, otherwise 0 will be added.
$F{created_at}.before($P{givenDate}) ? $F{value} : 0
To use a variable to sum, you need to change the calculation type to "Sum". The default reset type, report will sum values over the entire report. The other reset types work the same way just over different sections of the report (column, page or group).
Here is the XML for the "before" case:
<variable name="sumValueCreatedBefore" class="java.lang.Integer" calculation="Sum">
<variableExpression><![CDATA[F{created_at}.before($P{givenDate}) ? $F{value} : 0]]></variableExpression>
</variable>

there is another solution for that : write sub query in the select statment
like
Select
(select sum(Fieldname) from tablename where dategiven date) as aftersum
from tablename
where conditions

Related

Summations of values and showing result in the same row in iReport

I am using iReport designer. I have four columns in report. Value of one column will be calculated by doing sum summation and subtraction of other columns.
My result formula looks like this :
($V{cust_amount} == null ? new BigDecimal(0) : $V{cust_amount}).subtract( ($V{airlines_amount} == null ? new BigDecimal(0) : $V{airlines_amount}).subtract(($V{indi_amount} == null ? new BigDecimal(0) : $V{indi_amount}) ) )
But the result is coming just at the next row of the expected one. I am attaching a picture also.
Here the amount -15100 should come one the first row. But every value is coming just below the right row and first row is always null.
Your ternary expression seems fine - the problem is with value assignment to the variables you are using. The values are assigned after the first row is evaluated and either never change or they are assigned the same values over and over again.
You may fix value assignment to the variables but I recommend ditching the variables and using actual fields in the expression like this $F{cust_amount} - then you will be guaranteed proper value assignment before the row is printed.

Crystal Report Formular always return Boolean

iN my Crystal Report i have two columns of currency data types. I want to add a formular to the column a . i.e "WHEN A CURRENCY IN A is > THAN B, THEN The value of A should euqal the value of B ". I wrote my formula as below
currencyVar formular := {ProcName.coll};
IF({ProcName.coll} > {ProcName.ref})
Then
formular = {ProcName.ref}
AND
IF({ProcName.coll} > {ProcName.ref})
Then
{ProcName.coll}= {ProcName.ref}
Both yielded the same boolean values.When I saved and named the formula above, i then insert the formula to my column . However, the result are all boolean True/False. I am ot sure how this happened i check the data type of the formula is indicating boolean as well.
This is because you're testing equality not assigning the value as you wish to do. Change your last line to a statement of assignment by adding a colon before the equal sign:
...
formular := {ProcName.ref}
// ^

Conditionally sum in variable expression in total column

I'm building a report in iReport 5.0.4, so far I have a crosstab table as follows
What I'm trying to do in Total 4 is having a total of sum3Measure but only of types "X" or "Y" (defined by the value in $V{col1}). I tried in Text Field Expression this:
$V{col1}.equals("X") || $V{col1}.equals("Y") ? $V{sum3Measure } : 0
but it returns always null. I did:
$V{col1}.equals("X") ? $V{sum3Measure } : 0
to test if the operator || is not valid but it returns null as well.
I used $V{row1} instead of $V{col1} to test it wasn't a problem different from the expression, it did the sum.
All measures are defined with Calculation type Sum
Is it possible to do what I want? if not, is there a workaround I can try?
Any help will be appreciated, if you need more info just let me know.

Ignore nulls from multi-select input controls?

I have a report with several input controls that are used to populate another input control.
My input controls:
GPI - a multi-select input control that has product codes. The parameter is a collection.
NAME_LOOKUP - a single-value text box where the user can type a product name. The parameter is a string.
NDC - a list of drug codes that obtains values based on what is input in GPI and NAME_LOOKUP.
The query that populates NDC has this WHERE clause:
WHERE (
REGEXP_LIKE(DESCRIPTION, $P{name_lookup}, 'i')
OR REGEXP_LIKE(NDC, $P{name_lookup}, 'i')
)
OR ($X{IN, GPI, gpi})
It works as the $X syntax is designed to -- if the user doesn't select a GPI value from the list, my NDC input control shows all drug codes. However, I only want to show drug codes when a value is actually selected from the GPI input control.
When I try
WHERE (
REGEXP_LIKE(DESCRIPTION, $P{name_lookup}, 'i')
OR REGEXP_LIKE(NDC, $P{name_lookup}, 'i')
)
OR ($X{IN, GPI, gpi} AND $P{gpi} IS NOT NULL)
I get an invalid column type in JasperReports Server for the NDC input control.
When I try
WHERE (
REGEXP_LIKE(DESCRIPTION, $P{name_lookup}, 'i')
OR REGEXP_LIKE(NDC, $P{name_lookup}, 'i')
)
OR ($X{IN, GPI, gpi} AND $P!{gpi} IS NOT NULL)
I get a missing expression error.
What can I do to limit the NDC input control's results to just the selected GPI, and not all results?
I just found this while experiencing the same problem, I finally solved it using a second parameter holding the lenght of the list:
<parameter name="potentially_empty_list" class="java.util.Collection"/>
<parameter name="num_of_list_elements" class="java.lang.Integer" isForPrompting="false">
<defaultValueExpression>
<![CDATA[$P{potentially_empty_list}.size()]]>
</defaultValueExpression>
</parameter>
And the SQL query would then be:
[... select etc...]
WHERE $X{IN,mysql_column_name, ist}
AND $P{num_of_list_elements} > 0
which for an empty list would be sent to mysql as:
WHERE 0 = 0 #(that's how jasper parses $X{IN..} for an empty collection
AND 0 > 0 # don't match anything :)
There is an interesting alternative because $X{NOTIN, ...} also evaluates to 0=0 in case of empty list.
Therefore you can do like this:
($X{IN, cs.companyid, companyIds}
AND NOT
($X{NOTIN, cs.companyid, companyIds}))

String comparisons in JasperReports expressions

A database field named income_source is queried using:
SELECT * FROM table_name WHERE income_source LIKE "salaried%"
This retrieves income_source values with a "salaried" prefix. In iReport, the PrintWhenExpression value for the field is set as:
$F{income_source}.equals("Salaried")? Boolean.TRUE:Boolean.FALSE
Why does the report output differ from the SQL output?
There are a few problems:
The value "salaried%" in the SQL differs from the value of "Salaried" in the expression.
The value "salaried%" uses the % to match all text after the letter d.
There is a bit of redundancy in the PrintWhenExpression.
Try the following expression:
$F{income_source}.startsWith( "salaried" )
Or:
$F{income_source}.trim().toLowerCase().startsWith( "salaried" )
One of those should work. You will also want to ensure Blank when null is checked. Otherwise, the expression becomes:
$F{income_source} == null ? Boolean.FALSE :
$F{income_source}.trim().toLowerCase().startsWith( "salaried" )