Jump to content
We've recently updated our Privacy Statement, available here ×
  • Getting a previous record's value (for calculation in variables, etc)


    ghudson_1
    • Version: v5.5 Product: JasperReports® Library

    Issue Description:

    Occasionally you need to present some data or value of a field based on the previous record's value.  For example you might want to print the difference between the current and former value of a field.  Below is technique for implementing this example.


    Resolution:

    When needing the difference between the current record and the previous record, you need to use a extra few variables to find your comparison. Variables are incremented in the order they are declared, so you can exploit this order to keep track of the "previous" records value.

    Here's how this would work: 

    • You need two report variables: one to hold the current value and the  other to hold the previous value. The trick is to declare the "previous value" variable prior to the "current value" variable and make the first copy the value of the second  like this: 

      <variable name="INITIAL" class="java.math.BigDecimal" resetType="Report" calculation="Nothing">
          <variableExpression><![CDATA[$V{AAA}]]></variableExpression>
          <initialValueExpression><![CDATA[new BigDecimal(0)]]></initialValueExpression>
      </variable>
      
      <variable name="AAA" class="java.lang.Integer" resetType="Report" calculation="Nothing">
          <variableExpression><![CDATA[$F{FieldA}]]></variableExpression>
      </variable>
      
    • Of course if you are creating a calculation for comparison, then you need a third variable to calculate the difference between the two: 

      <variable name="DELTA" class="java.lang.Integer" resetType="Report" calculation="Nothing">
          <variableExpression><![CDATA[$V{AAA}-$V{INITIAL}]]></variableExpression>
      </variable>
      

    (note the you are taking the default values for resetType which is 'report', and default calculation type which is 'nothing')

    Attached is an example that works with our foodmart database. It just highlights the next row if previous value is not the same.


    Ref. Case #00042350

    deltavar.jrxml


    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...