ChristianNRW
ChristianNRW

Reputation: 542

iReport Variable for last Record

I have some records like this:

Datetime            | Type |
2012-02-23 22:00:11    1
2012-02-23 22:02:14    2
2012-02-23 22:03:56    1
2012-02-23 22:04:11    1
2012-02-23 22:33:21    2
....

I will generate a list which shows:

The Datetime field and, if Type = 2, the time difference between Last type 1 and actual Type2 Record. Formatted with HH:MM:SS

Example for the above mentionend Data:

Datetime            | Type |  Diff
2012-02-23 22:00:11    1
2012-02-23 22:02:14    2     00:02:03  (Diff to 22:00:11) 
2012-02-23 22:03:56    1
2012-02-23 22:04:11    1
2012-02-23 22:33:21    2     00:29:25  (Diff to 22:03:56)

I tried to use Variables in a different way.

But, as so often recently, unsuccesfull :-)

(i´m usinfg iReport 4.5.0)

Any ideas ?

thx Christian

Upvotes: 3

Views: 6755

Answers (3)

mdahlman
mdahlman

Reputation: 9390

First, create a variable $V{Diff} that calculates the difference between $F{Datetime} for all rows. This gives you the values you want (and a bunch that you don't need).

Second, only display $V{Diff} when $F{Type} equals 2.

But it's the details that make it fun. That variable is not entirely obvious in JasperReports. You actually need two variables, and they have to be defined in the correct order.

$V{Diff} Expression:          $F{adate}.getTime() - $V{LastType1Date}
$V{LastType1Date} Expresion: ($F{type}.intValue() == 1) ? $F{adate}.getTime() : $V{LastType1Date}

Then the print when expression is easy: $F{type}.intValue() == 2 And you get what you need: enter image description here

I have ignored the interval formatting, but I think this solves the key issues for you. You just need to transform the milliseconds into the format you want.

Upvotes: 1

Ben English
Ben English

Reputation: 3918

Create a Date variable called lastType1DateTime and for the variable expression put:

($F{Type} == 1) ? $F{DateTime} : $V{lastType1DateTime}

This variable will always hold the last type 1 date. Then simply do your diff in the detail field for type 2 rows.

Upvotes: 0

precose
precose

Reputation: 614

Try putting this in your variable expression (located in the variable's properties)

$F{Datetime}.equals($F{type}.equals(2))-$F{Datetime}.equals($F{type}.equals(1))

Upvotes: 1

Related Questions