Creation of a Crosstab


The first thing to is create a new report, from Jaspersoft Studio select File->New->Jasper Report, call the new report "TablesExample" and as template use "Cherry". As data adapter use "Sample DB - Database JDBC connection", that came with Jaspersoft Studio and as SQL query to discover the fields use: "select * from orders". Now you have a list of the discovered fields, add them to the report in this order: ORDERDATE, ORDERID, SHIPNAME, SHIPADDRESS and SHIPCITY. Don't use groups and complte the wizard. Now you have the report with some fields already placed in it. You can test if it is all right by compiling the report switching to the preview tab. If it all right you should see something like this:

Figure 1. Example Report

Example Report

Crosstabs are little more complicated of tables, even because its not easy to understand how the data is "crossed". Now we have a table that show the number of shipments done year by year. With the crosstab we can have something more informative, like the shipment done year by year, but also divided by the destination countries. So the goal is to have the countries on the rows, the years on the columns and in every cell of the table the number of shipment done for a precise country in a precise year. As first thing drag and drop a Crosstab from the palette into the designer and the corsstab's wizard will appear. In the first step you can choose if create a new dataset for the crosstab or use an existing one. For our purpose this time we can reuse the main dataset, so select "create a crosstab using an existing dataset", select "[Main Dataset]" and hit "Next".

Figure 2. Select the datasource type

Select the datasource type

Now you have to choose the fields that go on the columns. For our goal we need the shippeddate, but this is not really true because we need only the year and we have two ways to get this:

  • Create a new dataset or change the main dataset query to have another field that contains only the year, something like "select ORDERDATE, ORDERID, SHIPNAME, SHIPADDRESS, SHIPCITY, SHIPCOUNTRY, SHIPPEDDATE, YEAR(SHIPPEDDATE) as SHIPPEDYEAR from orders", that returns all the fields we need.
  • Using the main dataset but change manually the column element expression in the crosstab, from something like "$F{SHIPPEDDATE}" to "YEAR($F{SHIPPEDDATE})".

Probably the first way is more easy and clean, but since we already know how to create datasets and change the main dataset query, and we have never seen how to edit an already created crosstab we choose the second one to have a more informative tutorial. Returning to the wizard add the field "SHIPPEDDATE" by double clicking on it and hit "Next".

Figure 3. Select the columns

Select the columns

Now we must add the country on the rows, so in the next step select the field "SHIPCOUNTRY" and hit "Next".

Figure 4. Select the rows

Select the rows

It's time to define the detail data. Normally, the detail is the result of an aggregation function like the count of orders by country by year, or the sum of freight for the same combination (country/year). We choose to print the number of orders placed by adding the field "ORDERID" with the "Count" calculation (default value) and then click "Finish".

Figure 5. Select the measure

Select the measure

Now you have your crosstab created, change its size to fit the band and compile the report switching in the preview tab. Here you will see two problems:

  • Since we have used a the shipped date on the columns now we have a column for every day where there was a shipment, instead of every year;
  • Some elements dosen't have a shipped date, so there is a column for them with the label "null", it would be better to have the string "Undefined" (in a similar way to what happen with tables).

For the first problem go in the crosstab editor by doubleclicking on the its element. Here you see a dedicated outline view for this element, expand the element "Crosstab" and here you will see three very important elements: "Row Groups", "Column Groups" and "Measures". Here are placed the fields choosen for row, column and detail and to use this fields and are used elements of the type "RowGroup", "ColumnGroup" and "Measure" that are someway similar to variables since they use an expression that refer a field, they have a type and a calculation time.

Figure 6. Set the crosstab

Set the crosstab