Summations of values and showing result in the same row in iReport - jasper-reports

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.

Related

ADF dataflow: to assign null to a negative result

I have a requirement to load null if the total hours is less than previous total hours else the difference
iif(lesser(TOTAL_HOURS, PREVIOUS_TOTAL_HOURS),null(),TOTAL_HOURS-PREVIOUS_TOTAL_HOURS)
It gives me expression could not be evaluated.
Not all rows have values for these fields, some of them are null. They are numeric fields in database.
I just want to replace negative results with null
If you look at the document iif it says
iif(<condition> : boolean, <true_expression> : any, [<false_expression> : any]) => any
Based on a condition applies one value or the other. If other is
unspecified it is considered NULL. Both the values must be
compatible(numeric, string...).
Now as per your expression:
iif(lesser(TOTAL_HOURS, PREVIOUS_TOTAL_HOURS),null(),TOTAL_HOURS-PREVIOUS_TOTAL_HOURS)
since first value you have mentioned is of type null it expects TOTAL_HOURS-PREVIOUS_TOTAL_HOURS must also return a same type null
What you can try is:
iif(lesser(TOTAL_HOURS, PREVIOUS_TOTAL_HOURS),toInteger(null()),TOTAL_HOURS-PREVIOUS_TOTAL_HOURS)
OR
case(TOTAL_HOURS < PREVIOUS_TOTAL_HOURS, toInteger(null()), minus(TOTAL_HOURS,PREVIOUS_TOTAL_HOURS) )

What do lit(0) and lit(1) do in Scala/Spark aggregate functions?

I have this piece of code:
val df = resultsDf
.withColumn("data_exploded", explode(col("data_item")))
.groupBy("data_id","data_count")
.agg(
count(lit(1)).as("aggDataCount"),
sum(when(col("data_exploded.amount")==="A",col("data_exploded.positive_amount")).otherwise(lit(0))).as("aggAmount")
)
Is lit(0) referring to an index at position 0, or to the literal value of number 0? The definition I see at https://mungingdata.com/apache-spark/spark-sql-functions/#:~:text=The%20lit()%20function%20creates,spanish_hi%20column%20to%20the%20DataFrame.&text=The%20lit()%20function%20is%20especially%20useful%20when%20making%20boolean%20comparisons says that "The lit() function creates a Column object out of a literal value." That definition makes me think that it is not referring to an index position but to a literal value such as a number or String. However, the usage in count(lit(1)).as("aggDataCount") looks like referring to an index position of a column to me. Thank you.
lit(1) means the literal value 1
count(lit(1)).as("aggDataCount") is a way to count the number of rows (each row having a column with a value of 1 and summing this column)
In spark lit represents literal value.
lit(0)--> put 0 as a value in column , lit(1) --> means put 1 as a value in column.
In the code you shown above they are applying an aggregation over 2 columns and keeping a count of how many rows from count(lit(1)), over a condition.
The next lit(0) is in otherwise clause which is like an else condition. lit(0) will add 0 as a literal value in the column.

How to convert null rows to 0 and sum the entire column using DB2?

I'm using the following query to sum the entire column. In the TOREMOVEALLPRIV column, I have both integer and null values.
I want to sum both null and integer values and print the total sum value.
Here is my query which print the sum values as null:
select
sum(URT.PRODSYS) as URT_SUM_PRODSYS,
sum(URT.Users) as URT_SUM_USERS,
sum(URT.total_orphaned) as URT_SUM_TOTAL_ORPHANED,
sum(URT.Bp_errors) as URT_SUM_BP_ERRORS,
sum(URT.Ma_errors) as URT_SUM_MA_ERRORS,
sum(URT.Pp_errors) as URT_SUM_PP_ERRORS,
sum(URT.REQUIREURTCBN) as URT_SUM_CBNREQ,
sum(URT.REQUIREURTQEV) as URT_SUM_QEVREQ,
sum(URT.REQUIREURTPRIV) as URT_SUM_PRIVREQ,
sum(URT.cbnperf) as URT_SUM_CBNPERF,
sum(URT.qevperf) as URT_SUM_QEVPERF,
sum(URT.privperf) as URT_SUM_PRIVPERF,
sum(URT.TO_REMOVEALLPRIV) as TO_REMOVEALLPRIV_SUM
from
URTCUSTSTATUS URT
inner join CUSTOMER C on URT.customer_id=C.customer_id;
Output image:
Expected Output:
Instead of null, I need to print sum of rows whichever have integers.
The SUM function automatically handles that for you. You said the column had a mix of NULL and numbers; the SUM automatically ignores the NULL values and correctly returns the sum of the numbers. You can read it on IBM Knowledge Center:
The function is applied to the set of values derived from the argument values by the elimination of null values.
Note: All aggregate functions ignore NULL values except the COUNT function. Example: if you have two records with values 5 and NULL, the SUM and AVG functions will both return 5, but the COUNT function will return 2.
However, it seems that you misunderstood why you're getting NULL as a result. It's not because the column contains null values, it's because there are no records selected. That's the only case when the SUM function returns NULL. If you want to return zero in this case, you can use the COALESCE or IFNULL function. Both are the same for this scenario:
COALESCE(sum(URT.TO_REMOVEALLPRIV), 0) as TO_REMOVEALLPRIV_SUM
or
IFNULL(sum(URT.TO_REMOVEALLPRIV), 0) as TO_REMOVEALLPRIV_SUM
I'm guessing that you want to do the same to all other columns in your query, so I'm not sure why you only complained about the TO_REMOVEALLPRIV column.
What you're looking for is the COALESCE function:
select
sum(URT.PRODSYS) as URT_SUM_PRODSYS,
sum(URT.Users) as URT_SUM_USERS,
sum(URT.total_orphaned) as URT_SUM_TOTAL_ORPHANED,
sum(URT.Bp_errors) as URT_SUM_BP_ERRORS,
sum(URT.Ma_errors) as URT_SUM_MA_ERRORS,
sum(URT.Pp_errors) as URT_SUM_PP_ERRORS,
sum(URT.REQUIREURTCBN) as URT_SUM_CBNREQ,
sum(URT.REQUIREURTQEV) as URT_SUM_QEVREQ,
sum(URT.REQUIREURTPRIV) as URT_SUM_PRIVREQ,
sum(URT.cbnperf) as URT_SUM_CBNPERF,
sum(URT.qevperf) as URT_SUM_QEVPERF,
sum(URT.privperf) as URT_SUM_PRIVPERF,
sum(COALESCE(URT.TO_REMOVEALLPRIV,0)) as TO_REMOVEALLPRIV_SUM
from
URTCUSTSTATUS URT
inner join CUSTOMER C on URT.customer_id=C.customer_id;

Display a value in one column based on a value in another column

I have two columns:
Of Procs
Of Procs with a procedure time
If column 2 is blank, and column 1 has a value, then I need column 2 to display a zero. What would a formula look like for that?
Use this formula
if ({Of Procs}="") then "0"
else {Of Procs}
I tested it and it works.
Hope it helps
if (isnull({TABLE.column 2}) or {TABLE.column 2}="")
and
(not(isnull({TABLE.column 1})) or {TABLE.column 1}<>"")
then
"0"
else
{TABLE.column 2}

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.