Row counter in sub report - Jaspersoft - jasper-reports

I have a subreport that receives two parameters and therefore generates as many reports as there are combinations of parameters, I have to put a row counter in the sub report but this must not reset every report generated by the sub report but must continue the numbering.
This is a conversion from a crystal report
In crystal report do this:
shared numberVar J;
if trim({field}) <> '' then J:=J+1;
I have a subreport to which I pass in the sql query two parameters whose values ​​are generated by the main report, the combination of these parameters in the subreport generates a series of reports in the main report because the subreport is in the detail band.
In the subreport I have to insert a variable that counts the number of rows, but when I insert this variable the numeric counter is reset and starts counting in the new report generated by the subreport, I would like the counter not to reset
Main report
Detail of the band
subreport
Subreport
report 1
1 row
2 rows
3 rows
report 2
1 row
2 rows
3 rows
I would like to have
- report 1
1 row
2 rows
3 rows
-report 2
4 rows
5 rows
6 rows
I hope I was clear
Do you have any ideas?
Can someone help me?
Please
Thanks a lot to everyone

You can pass back and forth a running row count variable to the subreport using parameters and return variables.
You'd need something like this:
(in the master report)
<!-- variable that stores the running row count in the master report -->
<variable name="SubreportRowCount" class="java.lang.Integer" calculation="System">
<initialValueExpression>0</initialValueExpression>
</variable>
...
<subreport>
...
<!-- pass the current value to the subreport -->
<subreportParameter name="InitialRowCount">
<subreportParameterExpression>$V{SubreportRowCount}</subreportParameterExpression>
</subreportParameter>
...
<!-- get back the updated value from the subreport -->
<returnValue subreportVariable="RunningRowCount" toVariable="SubreportRowCount"/>
...
</subreport>
(in the subreport)
<!-- parameter that receives the initial value from the master -->
<parameter name="InitialRowCount" class="java.lang.Integer"/>
...
<!-- running row count variable -->
<variable name="RunningRowCount" class="java.lang.Integer">
<variableExpression>$P{InitialRowCount} + $V{REPORT_COUNT}</variableExpression>
</variable>
...
<!-- show the running count in a text element -->
<textField>
...
<textFieldExpression>$V{RunningRowCount}</textFieldExpression>
</textField>

Related

How to create grouped Cross Table in Jasper reports in different excel tabs?

What I have is single cross table shown below whose data is coming from a sub data set.
I would like to split this cross table on the basis of column1 and want something of this sort.
Further, I would like to export these tables in different excel tabs. May be by dynamically adding page break between them (Still figuring out how to do that as well).Name of these tabs has to based on Column1 i:e type1, type2, type3 etc. How do I dynamically set those names.Lastly, I also have to add this report to a another main report as sub report. Keeping all this in mind. Any help on how do I achieve this sort of format?
After struggling alot I was able to do it in this way.
Use report Dataset(main) for the crosstab.
Create group on Column1. Add footer for it.Remove all other band if you don't want them.
Put the break then Crosstab then a dummy Label as shown in the group footer.
4.Use it as subreport in different report.
Important Explanations:
Label is basically for naming the exported tab in excel.
<staticText>
<reportElement x="0" y="4002" width="100" height="20" uuid="392f134e-746d-46bf-8895-ecb403d54211">
<propertyExpression name="net.sf.jasperreports.export.xls.sheet.name"><![CDATA[$F{COLUMN1}]]></propertyExpression>
</reportElement>
<text><![CDATA[]]></text>
</staticText>
Here i utilized the dynamic property expression to name the sheet dynamically and Break to split the sheet in different tab on group to group the data based on column1.

Jasper Sum isn't including the last value

I am using Jaspersoft Studio 5.6.2 and have a report with a column showing elapsed days between two dates.
I want to show at least one day if the date range is less than 24 hours so I created a variable to convert the field value to indicate that. I also want to avoid negative values:
Name: day_rounded
Class: java.lang.Integer
Calculation: No Calculation
Expression: $F{days}==0?1:$F{days}<0?0:$F{days}
Increment: None
Reset: Report
Here is the variable I created to total them:
Name: totals
Class: java.lang.Integer
Calculation: Sum
Expression: $V{day_rounded}
Increment: None
Reset: Report
$F{days} is the difference between two dates from the query.
...
DATEDIFF(dd,irr.xxxstart_dt, ISNULL(irr.xxxend_dt,$P{end_dt})) AS days
...
I ran the report with the $F{days} value alongside the $V{day_rounded} value and they look OK.
When i put the $V{totals} into the Summary band (it is in the Page Header as well) the value does not include the one on the last row. For instance if I use a calculator and sum up all the days listed I get 1494. The report shows 1485 and the days value for the last row is 9 - so I can see it is not including that one. I ran the report for other date ranges and the total always is exactly the value in the last row less than it should be.
As far as I can tell the variables are declared correctly. Any idea why I am missing the final row value in the sum?
Here is the jrxml I used to solve this:
<variable name="page_total" class="java.lang.Integer" resetType="Page" calculation="Sum">
<variableExpression><![CDATA[$V{day_rounded}]]></variableExpression>
</variable>
<variable name="paged_summed" class="java.lang.Integer" incrementType="Page" calculation="Sum">
<variableExpression><![CDATA[$V{page_total}]]></variableExpression>
</variable>
I totaled my days values per page and had the RESET set to 'Page' for that variable. I summed them up and set the INCREMENT to 'Page for the other variable.
Now my totals are all adding up. Thank you #tobi6!!

Subdataset - total rows in the List

I have created a subdataset in my main report and using that to create a list. The list displays all the rows from my dataset, but is there a way to place a total number of rows count in the list component.
With the $V{REPORT_COUNT} variable, i can get the total count but its repeated multiple times (as the number of rows in the sub dataset )
As noted in the answer by user jmurray here: http://community.jaspersoft.com/questions/514827/row-count
In your subDataSet, create a variable:
<variable name="ROW_COUNTER" class="java.lang.Integer" incrementType="Report" calculation="Count">
<variableExpression><![CDATA[new java.lang.Integer(1)]]></variableExpression>
<initialValueExpression><![CDATA[new java.lang.Integer(0)]]></initialValueExpression>
</variable>
Then somewhat like what Mike Noland has in his answer, create a list that points to the same subDataSet as the original list with a textField:
<textField>
<reportElement isPrintRepeatedValues="false" x="0" y="0" width="100" height="11"/>
<textFieldExpression><![CDATA[new java.lang.Integer($V{ROW_COUNTER}.intValue()+1)]]></textFieldExpression>
</textField>
This creates a zero based counter that holds the total count of items. Then by telling the report not to print repeats, you end up with a single item that represents the total number of items in your subDataSet
Create A second List set it to use the same dataset and put only the $V{REPORT_COUNT} in it. Set the position type to float
Second option is to use the Table in place of the List as it has the banding to allow for a header or footer of your dataset

Conditional Executing subreports in iReport

Am using more than 100 subreports and some Detail band in my main report
And given conditional Visibility of the sub reports / Detail Band based on the Parameter selection in main Report
<subreport>
<reportElement uuid="9f36c3cb-7e29-4040-a14e-6a91775e89e4" x="0" y="35" width="555" height="47">
<printWhenExpression><![CDATA[$P{disp} == 1]]></printWhenExpression>
</reportElement>
. But the performance is very very slow in main report.
While selecting single sub report executes in seconds. The same report in main report executes in hours.
According to my view,
All the Sub reports and detail Band executes and finally conditional visibility is working. Can we Control executing sub reports?
you could possibly use an own detail band for each subreport, and set each with the "print when expression". like this, the subreport should not be executed at all.
in case you are using datasets in the main report for each subreport (however that should work), move the queries to the subreports.

Add a variable to column footer iReport

I want to add a variable to column footer of my table. It is the sum of the column values. eg: total sales, if column name is sales.
But when i try it it says variable not found for some reason.
I have positioned the table in title band because if i put it in detail band the table repeats multiplied by the number of rows in the table.
5rows = 5pages with the same table.
Can someone please help me since im new to jasper and ireport.
I am using ireport 5.0.0
Thank you
Make sure you have this on your xml
<field name="total_column" class="java.lang.Long"/>
and have this as your text field expression
$F{total_column}
Change the name to whatever you are using.
We always define variables at the center level so make sure if you are using any sub data-set for your table component, copy the same query in main report query area, then add a variable
and change these properties in Ireport :-
Variable class :- java.lang.integer or java.math.BigDecimal
Calculation :- Sum
Variable expression:- $F{total_sales}
XML:- if you are not using Ireport
<variable name="total_sales" class="java.math.BigDecimal" calculation="Sum">
<variableExpression><![CDATA[$F{total_sales}]]></variableExpression>
</variable>
and after this you can use this variable for sum.