A: 

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

medopal
Thanks for responding, medopal. The major problem is that you cannot simply add variables like <code>$V{a} = $V{a} + $V{b}</code> because $V{a} is actually a shortcut for something like <code>variable_a.value()</code> so it is read-only.I just want to get the grand total in the report summary band at the end of the report.The second problem is that I can't seem to initialize the variable (with <code>Initial Value Expression</code> to 0 — Not even with "new Integer(0)"
sventech
im very sure i initialize using new Integer(0) in iReport, but not sure about the $V = $V1 + $V2 though.Anyway, i would suggest two ways:1- Find the grand total in your sql query or back bean and pass it as parameter. I honestly never trusted how Jasper deals with the return variable.2- In Jasper you can always link to a Java method of your own code.Make a method with static identifier. lets say public static void myReportRecordCount(int i){ staticGrandTotal+=i;} call this method from the sub report details section.in the summary band, put a method to read the static variable.
medopal
Re: 1- Yes, a more complex SQL query in the main report that does what the subReport does over again might be possible, but this report already takes 5 minutes to generate, so I don't want to push it. It will make my product unusable if it takes too long to run. Re: 2- I'm reporting from a standard JDBC database connection, and I really want my customers to be able to use and modify the reports with iReport without having to add my custom code to their CLASSPATH.
sventech
5 minutes? i have made few statistical reports, they were very complicated and printed on A3 papers, with 100s of pages, the query never took 5 minutes!! you might want to tune your queries.About my first suggestion, i was suggesting something like select count(x) from table1 where group_one,group_two.... conditions. Then send this variable to the report. The select count should not make lot of overhead, its a light query.
medopal
Your assurance about 'new Integer(0)' was quite valuable. It turned out to be a flakiness/bug issue in iReport 3.6.0. As for the time required for report generation, the datasets I've been working on are quite large ( >300,000 rows with ~30 columns ) and running an a poorly tuned Microsloth SQL Server 2000 instance.
sventech
+2  A: 

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"/>
cetnar
Thanks! I initially started down this path, but iReport gave me a bogus error whenever I added an initial value -- it said that the class of the initialValueExpression was not compatible with the class of the variable (when they were 'new Integer(0)' and 'java.lang.Integer' respectively) -- the trick was to compile the report, close it, and reopen it. I smell a bug. :-)
sventech
+1  A: 

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.

Jenny Smith
That's a cool idea, but not quite as straight-forward as I'd like for what I'm trying to do. Thanks for your contribution! It helped me to think about the problem differently.
sventech