Display a Sum on the Next Page

Question

I need to display previous-page sum value (Amount of items sold) in the current page. If it is the first page then there’s no such display. From the second page onwards the total sum value of the previous page should be displayed at the top.

Ex : Page : 1

1.2 Product_Name Amount

Product1 100

Product2 200

Page : 2

1.3 Product_Name Amount

Pre.page_sum 300

Product3 400

 

Answer

JasperReport can’t calculate the sum of previous page at print. You can calculate sum for each group, union them together to insert the previous-page sums into the report data source in order during data preparation, leaving the reporting tool handle the paging only. It’s easy to do this in SPL (Structured Process Language).

A

1

=$select   Product,Amount from tb

2

=$select   Product,Amount from tb

3

=A2.new(“Pre.page_sum”:Product,~.sum(Amount):Amount)

4

=A2.conj(~|A3(#))

A1: Data retrieval.

A2: Group every (page_Row-1) (the parameter representing a number) rows.

A3: Sum each group and create a new table sequence.

A4: Union each group and its sum and return it to JasperReport for paging.

You can connect to esProc in JasperReport via JDBC. Calling an SPL script is the same as calling a stored procedure. For more information, see How to Call an SPL Script in JasperReport