CSM CSM
CSM CSM

Reputation: 23

How to repeat the grouped filed horizontally (jasper report)

DB Table - RunRecord

Name      Student_ID   Meter(m)   Time(s)
ABC       1016         100        13   

ABC       1016         200        26

ACB       1017         100        15

BAA       1018         100        18

BAA       1018         200        22

BBB       1019         100        14

CDE       1020         200        22

CDE       1020         100        14

What should I do so that I can get the following result in Jasper Report?

                      Student Run Record

                  ABC           ACB           BAA        BBB         CDE
100M              13            15            18         14          14
200M              26                          22                     22

Upvotes: 0

Views: 2833

Answers (2)

Dimuthu Roshan
Dimuthu Roshan

Reputation: 1

Change the bucket expression of report row group. Make the bucket expression by concatenation of few fields. eg:

<bucket class="java.lang.String">
    <bucketExpression><![CDATA[$F{field1}+""+$F{field2}]]></bucketExpression>
</bucket>

Upvotes: 0

MrsTang
MrsTang

Reputation: 3119

You can achieve this by using a crosstab element.

I used the query as provided by you, I have in my sample these fields:

  • Name: String
  • Meter: Long
  • Time: Long

Drag and drop the crosstab from the Palette into the summary band of the report in the Report Designer, a wizard will pop up.

Follow the wizard:

  • Select the dataset that holds the query.
  • Configure as rows Meter with grouping set to unique, as column you choose Name with grouping also set to unique.
  • As measure you use Time, and change the aggregator to Sum (anything but count, as you only have one value per student and distance the sum will be the same as the value).

Once the crosstab is added click in the Report Inspector and expand the crosstab, click on row groups > Meter and in the Properties panel select as Total Position:None. Same for Column Groups > Name.

The output will be then as shown in the image below.

draft report

In order to get the output closer to what you drafted in your query:

  • In the Report Inspector select Row Groups > Meter, in the Properties panel change the Bucket Value Class to java.lang.String. Then in the designer edit the field that holds $V{Meter} and change it to $V{Meter} + "m" to have output 100m instead of 100.
  • In order to hide 0, click in the Designer on the field that holds the measure and change it from $V{TimeMeasure} to $V{TimeMeasure} == 0 ? null : $V{TimeMeasure}. In the Properties Panel check the box for property Blank when null.

finished report

Upvotes: 3

Related Questions