How to get sum of entire field? - jasper-reports

I'm going to get the sum of a entire field in Jasper Report. I'm passing JRTableModelDataSource from a Java code.
Table
Unit Price | Quantity
100.00 | 5
150.00 | 2
200.00 | 4
I'm printing these values in my report using field.
Field Expression
$F{Unit Price}*$F{Quantity}(This is the variable named '$V{MUL}')
and it prints
500.00
300.00
800.00
And now I need to prints the sum of all these values such as,
500.00
300.00
800.00
------
1600.00
I need to print this 1600.00(Sum of entire Field) in my report.
I tried with this,
SUM($V{MUL})
$V{MUL} is the variable that of multiplied values(500.00,300.00,800.00)
But I got an error. Is this possible to do or How can I do this?

You can do as shown below
<variable name="GrpAmount" class="java.math.BigDecimal" resetType="Group" resetGroup="keyType" calculation="Sum">
<variableExpression><![CDATA[$F{Unit Price}*$F{Quantity}]]></variableExpression>
</variable>

Related

SSRS 2012 Mean & Median

I have a report that i use Mean and Median measures that were calculated in SSAS 2012 tabular, the reports as follows:
when i use the Mean and Median directly, the values in the green box are correct and the column and row totals are incorrect.
after using aggregate instead of sum the following happens:
1- the blank row and blank column are now gone along with their values.
2- Then total of the Mean is correct in every cell except the grand total(2nd cell from right bottom corner), it appears to take the value of the cell given in Mean for the cross between blank and blank in previous pic.
3- The totals for the median are now either blank or 0 except for the grand total (right bottom corner cell), it seems to have the value for the Median from the cross of blank and blank in previous pic.
I am stuck here i dont know what to do and i want to avoid using another dataset if possible. HELP!!
A screen shot of my Query designer:
After including all empty spaces :
(=Aggregate is used on every cell),
please note after taking away extra dimension columns, the Total row in the bottom changed , the correct Mean totals from pre-pic are now gone.
The MDX for the DataSet :
SELECT { [Measures].[INCOME AVERAGE], [Measures].[INCOME MEDIAN] } ON COLUMNS, {[DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[EDU_TYPE_ENAME].ALLMEMBERS * [DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[NATIONALITY_L1_ENAME].ALLMEMBERS, [DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[EDU_TYPE_ENAME].ALLMEMBERS * {[DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[All]}, {[DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[All]} * [DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[NATIONALITY_L1_ENAME].ALLMEMBERS, {[DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[All]} * {[DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[All]}, ([DIM_Type of Education for Household نوع التعليم لرب الأسرة].[EDU_TYPE_ENAME].[EDU_TYPE_ENAME].ALLMEMBERS * [DIM_Nationality of household الجنسية لرئيس الأسرة].[NATIONALITY_L1_ENAME].[NATIONALITY_L1_ENAME].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM [Model] CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
Thanks in advance.
Your MDX query includes a ton of fields. Can you simplify it to the following to see if it solves the problem:
+-----------+-------------+--------+---------+
| Education | Nationality | Median | Average |
+-----------+-------------+--------+---------+
| null | null | 100 | 110 |
| PhD | null | 100 | 110 |
| null | Saudi | 100 | 110 |
| PhD | Saudi | 100 | 110 |
+-----------+-------------+--------+---------+
Don't return any extra dimension columns.
I am using null to represent the All member which is now the MDX query designer does it. Hopefully just dragging and dropping Education and Nationality onto the grid in the graphical MDX query designer will get you what you want. There is a button that turns on showing the All member if I recall.
Row 1 is for the grand total in the very bottom right.
Row 2 is for the subtotal in the far right column.
Row 3 is for the subtotal on the bottom row.
Row 4 fills up the body of the Report.
Then change the layout of the Tablix so that the row group is on the Education column and nothing else. Change the column group to be on Nationality and nothing else. Make sure all the textboxes in your Tablix have =Aggregate. I am hopeful it should work now.

Jasper Reports - calculation of total using CalculationEnum.System

I have 3 columns (obtained from the query along with the data)
| A | B | C |
| 5 | 15 | 20 |
| 15| 25 | 40 |
The net total for these columns are to be calculated as below :-
20 40 0.5 (i.e , the total of C = total of A / total of B) . How do I get the total calculated as required in jasper reports using java .
For calculating the total of column 'C' , I have set the calculation enum constant to be calculationEnum.SYSTEM . On setting the expression to be
expression.setText("new Double($F{" +A + "/" +B +"}.doubleValue())"); , it shows that the corresponding field , new Double($F{A/B}.doubleValue()) does not exist . To eliminate this , I have added the field in the list of field columns . But then it shows that the column for the corresponding field doesnot exist .
Is what I have done until now correct ? Or is there any better way in which I can get the total to be calculated as required .
Field A and B need to be of class="java.lang.Number" or one of its sub classes as java.lang.Double
To get the sum of them the expression is:
$F{A}.doubleValue() + $F{B}.doubleValue()
To calculate the SUM($F{A}) / SUM($F{B}) you need to use two variables
<variable name="sumOfA" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{A}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
<variable name="sumOfB" class="java.lang.Double" calculation="Sum">
<variableExpression><![CDATA[$F{B}]]></variableExpression>
<initialValueExpression><![CDATA[0]]></initialValueExpression>
</variable>
And then in the textField do the division, remember to set evaluationTime="Report" on the textField so that the variables have been calculated before displaying.
<textField evaluationTime="Report">
<reportElement x="208" y="17" width="100" height="20" uuid="414e6633-58c5-4081-a04b-fe3973f29d96"/>
<textFieldExpression><![CDATA[$V{A}.doubleValue()/$V{B}.doubleValue()]]></textFieldExpression>
</textField>
Create two variables, one for sum of A and one for sum of B.
And then add a text field with the following expression:
($V{sumOfA}/$V{sumOfB})

Variable value not getting printed in Detail band

I am working on a jasper report which uses following query:
select * from user_type
columns:
user_type
visit_date
sessions
user_type have values either "New Visitor" or "returning Visitor".
To get count of sessions of "New Visitor" I am creating a variable as:
<variable name="new_visitor" class="java.lang.Long" calculation="Sum">
<variableExpression><![CDATA[$F{user_type}.equals("New Visitor") ? $F{sessions} : 0]]></variableExpression>
</variable>
But when I print value of this variable in detail band or summary band its printing only zero.
Can someone please tell me what I am doing wrong here?

Jasper - How to remove complete row when 1 perticular field is null?

I want to print pdf something like this
Name Class RollNo
------- ---------- -----------
John 5 <null>
Mark 5 103
Robert 6 104
I need to add condition if RollNo is null then remove that row in 'detail' band.
You can use the report's filter expression or the detail band's print when expression. The filter expression completely skips the record, which is not counted and does not participate in aggregations, while the band's print when expression simply inhibits the band from printing.
<filterExpression>$F{RollNo} != null</filterExpression>
...OR...
<detail>
<band height="x">
<printWhenExpression>$F{RollNo} != null</printWhenExpression>
<textField>
...

How can I increment a variable with value of another variable in JasperReports?

I need to make a grand total of the items I'm counting in a subReport. To do that, I think I need to add the value of that variable to another variable for each iteration, or "increment" it by that value. The subReport gets called for each group, and I get a total for that group. I need to add the variable values, rather than database columns/fields.
I'm receiving an integer returnValue from the subReport, which is itself the count of rows in the sub-report. I want to get the grand total, since that subReport is called multiple times for the different results (each for a GROUP) from my main SQL query. I want to add up all the results, but I'm getting a null value. I tried adding an operation to the subReport as a new returnValue and choosing Sum as the operation, but that also yielded a null.
<variable name="itemCount" class="java.lang.Integer" resetType="None"/>
<variable name="grandCount"
class="java.lang.Integer"
incrementType="Group"
incrementGroup="ITEM_BUNDLE">
<variableExpression><![CDATA[$V{itemCount}]]></variableExpression>
</variable>
...
<returnValue subreportVariable="countItems" toVariable="itemCount"/>
Add attribute calculation="Sum" to variable name="grandCount"
or pass grandCount to subreport as parameter
<subreportParameter name="grandCount">
<subreportParameterExpression><![CDATA[$P{grandCount}]]></subreportParameterExpression>
</subreportParameter>
in subreport declare variable countItems with initialValue of parameter grantCount
<variable name="countItems" .... >
<variableExpression><![CDATA[$P{itemCount} + $P{grandCount}]]></variableExpression>
<initialValueExpression><![CDATA[$P{grandCount}]]></initialValueExpression>
</variable>
and return
<returnValue subreportVariable="countItems" toVariable="grandCount" calculation="Sum"/>
Im not exactly shure how to write it in JRXML since i use iReport.
In iReport, i create a new Variable, with class type "Integer", and calculation type "System"
The calculation type is important here.
In the variable expression, you will need something like $V{grandCount} = $V{grandCount} + $V{itemCount}
NOTE: JasperReports render band by band, so you wont be able to use the grandCount variable in a band before the subreport band.
Hope im not too late
You might try to increment your variable (I named it totalSum) only when the band (group) is equal to the one on which the subreport is. For this you would need a field in the report to give you the current band (group).
<variable name="totalSum"
class="java.lang.Integer"
resetType="Report"
incrementType="Group"
incrementGroup="ITEM_BUNDLE"
calculation="Nothing">
<variableExpression>
<![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : $V{totalSum}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
I'm not sure if this works, I don't have the context to test it. But you might also try a second solution - with three variables. For example, you keep the value returned from the subreport (let's say returnValue) in a variable and you use another two variables to hold the sum - one until the subreport is called (let's say partialSum) and the second to store the sum between the returnValue and the partialSum. Let's call it totalSum. Then you would have something like this for totalSum:
<variable name="totalSum"
class="java.lang.Integer"
resetType="Report"
incrementType="Group"
incrementGroup="ITEM_BUNDLE"
calculation="Nothing">
<variableExpression>
<![CDATA[$V{returnValue} + $V{partialSum}]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
For partialSum, you'll have something like this:
<variable name="partialSum"
class="java.lang.Integer"
resetType="Report"
calculation="Sum"
incrementType="None">
<variableExpression>
<![CDATA[new Boolean($F{reportPart}.equals("The_band_with_the_subreport")).booleanValue() ? $V{returnValue} : new Integer(0)]]>
</variableExpression>
<initialValueExpression>
<![CDATA[new Integer(0)]]>
</initialValueExpression>
</variable>
I hope this helps a bit. It would be easier to make all these settings from iRport directly on the report you want to use.