How to show column at Crosstab even the data is absent

后端 未结 1 839
北荒
北荒 2021-01-22 05:13

Do anyone have hints, if I want to create crosstab columns from array, then insert the correction information into the crosstab?

For example, I have a table like below i

1条回答
  •  感情败类
    2021-01-22 05:29

    To show dates without dates (without activities in your case) you should pass the data with datasource. The JasperReports does not know anything about any ranges of dates or something else. It is just require a data.

    The problems

    • The first problem is to get data in date range even the data is absent
    • And the second one - is to avoid showing in crosstab 'null' data

    Solution

    • In case using DB you can use outter join and some date range 'generator' to show the data.

      We should solve this tasks:

      1. Getting the list of all dates (days) in some period of time. For different RDBMS the syntax will be different.

        For PostgreSQL you can find the solution in Getting date list in a range in PostgreSQL post
        For MySQL - MySQL how to fill missing dates in range & generate days from date range For SQL Server - SQL Server: How to select all days in a date range even if no data exists for some days

      2. Using left or right outter join.

      3. Sorting data by date and data you want

    • In case using JavaBean datasources you hould do the same - add dates you need (without data) and sort data. We can skip implementing the data sorting and ask the JasperReports engine to do it for us.

    • The crosstab has one 'feature' - we can't hide the row with condition. Even we set all properties for hiding all textFields - the empty row will be drawn. If we try to use a filter on Crosstab our additional rows will be disappear. I think the good idea (in this pitiful situation) is to have special name for such row (we can also replace 0 with something better). In my sample it will be 'Not set'.

    The sample

    I've used the PostgreSQL in this sample.

    The report's template