condition sum (Compared row between them)

I need to calculate how  much cars has been left parking and how many arrived.

How do you compare two lines between them? How do I write expression?


date occupied


left car : 1


arrived: 1


thank you.

Best regards.


pixar's picture
Joined: Feb 20 2015 - 2:19am
Last seen: 5 years 3 months ago

Oups I did not ask if you were trying to achieve this using Jaspersoft Studio?

plambert_1 - 7 years 10 months ago

If there’s data recording multiple days of information of the usage of a parking garage (for example, 73,74,73,70 for a certain day), and we need each day’s numbers of cars that get into and out the garage, we can do the calculation this way: Group the data by dates; in each group we subtract the number of parked cars in each member from this number in the next member (73,1,-1,-3), get the remainders from the second member to the last (1,-1,-3), and then, to get the number of cars that get into the garage, calculate the sum of the numbers that are greater than 0 (1=1), and to have the number of cars that go out of it, calculate the sum of the numbers that are less than 0 and get the opposite number ((-1-3)*-1=4).

This is relatively complicated, so we’d better generate a two-dimensional table according to this algorithm to make the data source ready. Then we can just present the result set in Jasper. As it’s difficult to implement it in SQL, we could do it in esProc with simpler code: 

A1=$select date,occupied from tb;t=(~.(occupied-occupied[-1]).to(2,)),>0).sum():arrived,<0).sum()*-1:left)

Like calling a database, Jasper can integrate the esProc code via JDBC.

calculate.machine - 7 years 10 months ago

1 Answer:

You will need to use Groups and Variables if you do not want to use SQL.

  1. Create a group with an expression : $V{REPORT_COUNT}%2+1
  2. Create a variable : ($V{REPORT_COUNT}%2==1)?$F{occupied}:-$F{occupied}  (assuming occupied is an Integer)
  • Calculation: SUM
  • Incrementation : Group
  • Reset : Report

Then Drag the Variable into the Group footer (or Detail) set the Evaluation Time to Group you will get the expected result.

FYI $V{REPORT_COUNT} is like a row count .

plambert_1's picture
Joined: May 21 2013 - 10:18am
Last seen: 4 years 11 months ago