Column-to-row Transposition

Question

I have a long report which shows a lot of KPIs with columns for today, yesterday, last month, year to date etc.

Each is a sub report which returns one row with all the required KPIs for the given period, e.g.

registrations

deposits

games

3

1

23

 

There are about 30 of these KPIs (i.e. 30 column headers in the sub reports)

The report will look like this:

kpi

today

yesterday

this month

last month

this year

registrations

3

4

19

42

333

deposits

1

1

12

13

111

games

23

24

29

22

23

etc

 

The problem is in the sub report. I have to explicitly create a field for each KPI, and then explicitly create a text box going down the page for each field.

Is there no way to simply list the data which comes back from the query going down the page? I know there is a table component, but this only works if you have a single query which returns multiple rows, I have multiple queries which return a single row each.

 

Answer

The source data format isn’t one required by the reporting tool. That makes your reporting difficult. We can make some changes. Union “rows with all the required KPIs” as a table with the following format in SQL:

 

range

registrations

deposits

games

today

3

1

23

yesterday

4

1

24

thisMonth

19

12

29

lastMonth

42

13

22

thisYear

333

111

23

 

The transposition of the source data makes the displaying much easier. You can transpose a SQL result set of any structure in SPL in JasperReports. Here’s the SPL code:

A

B

1

=myDB1.query("select *   from  KPISubtoal")

2

=A1.fname().to(2,)

3

=create(KPI).record(A2)

4

for A1

=columnName=A4.#1

5

=A4.array().to(2,)

6

=A3=eval("A3.derive(B5(#):"+columnName+")")

 

A1: Retrieve data in SQL;

A2: Get field names from A1’s result set and return them beginning from the second one;

undefined

A3: Create a new table sequence;

A4-B6: Loop through A1 to populate the corresponding values to A3’s table sequence.

You can connect to esProc in JasperReport via JDBC. Calling an SPL script is the same as calling a stored procedure. Details are explained in How to Call an SPL Script in JasperReport