Pages

Wednesday, October 13, 2010

creating a crosstab report with jasper report

creating a crosstab report with jasper is became very easy task now...
as I explore it I found it very easy and simple to do...
the most tedious task was earlier to manage with the query to get the data in cross format. since there was no alternate or easy feature available from database either.
but for now with japserreport it become so easy that you don't have to modify your query at all just fire simple query and its done.
lets see how.

to show example I will use hssqldb that shipped with ireport tool.

concept: in a business case we have some times a requirement where we need a crosstab data for analysis which is difficult to achieve. because what we store in database is in number of rows and if we need that to be shown in columns is very difficult to achieve.

in our example lets see following result of table "tasks"

and assume now that we have a requirement to show this data in a different format as
group scheduled and actual and subgroup with tasks within and show them as columns
and subtasks as rows. that means it will look like something like this

                actual                           | scheduled
               task1 | task2 | task3      |task1 | task2 | task3     
 subtask1 
subtask2
subtask3
etc.

lets see how we can put this in cross tab report as below.
1. open ireport
2. file>>new>>launch report wizard (click) button
3. give reportname.>> next>>
4. select database connection>>add sql query as "select * from tasks" >> next>>
5. select all fields >> next>>
6. next>> finish>>
7. set all bands height as 0 except summery band.

8.   drag and drop crosstab icon on summery band.
9.   select "main report databaset" >> next
10. in rows we want to show subtasks so select "subtask Field" in rowgroup1 , group by "unique" >> next>>
11. now for column we have two level of grouping first is actual and scheduled and under it tasks so for
columngroup1 select "series Field" group by "unique">>
columngroup2 select "task   Field" group by "unique">> next>>
12. for data we will select "percent Field" , function "nothing" because we want to show values as it is.>> next>>
13. you can choose colour scheme of your choice and also column/row grouping if you want for my example i am leaving it unchecked that means I am not using row& column grouping.
 >> finish.

Thats all your report is done now click on preview and try to see how it looks.

after genrating view see it as below.


 


25 comments:

  1. Hi,
    Thanks for the write up...Helped me in grasping the functionality...

    ReplyDelete
  2. Thanks. It was really useful.

    ReplyDelete
  3. Thank u...it is very easily understandable....

    ReplyDelete
  4. Hi,

    I am trying to use crosstab to generate the complex report.
    The report format is something like given below,

    Constraint Name: ABC Constraint:Used for: ABC Purpose
    --------------------------------------------------------------------------
    Bin |2012-11-01 00:00 | 2012-11-02 00:00 | 2012-11-02 00:00 |
    --------------------------------------------------------------------------
    Production |200 |344 |11 |
    --------------------------------------------------------------------------
    Min Limit |200 |400 |15 |
    --------------------------------------------------------------------------
    Max Limit |210 |500 |20
    -------------------------------------------------------------------------

    Here the values varies each time for different constraint given as the title.

    Can you please suggest? |

    ReplyDelete
    Replies
    1. Hi I hope it will work, values are populated dynamically as a part of variable choosed in cross tab.
      you please try to follow my steps and try to prepare the report if you face further problems please dont hesitate to ask.

      Delete
  5. hi itried this.. this was really helpful
    but i hve an error
    net.sf.jasperreports.engine.design.JRValidationException: Report design not valid :
    1. Field not found : shopid
    2. Field not found : tarid

    ReplyDelete
    Replies
    1. Hi This means that you did not added a fields which you will like to read from query or database, pls verify the fields created which is shown in step 4,5 also make sure you are using proper alias or field names in query

      Delete
  6. Hi,

    I use crosstab from sql query
    select
    sum(this_.PaySum) as y0_,
    c3_.contragentname as y2_,
    org.shortname
    from CF_ClientPayments this_
    left join CF_Contragents c3_ on this_.IdOfContragent=c3_.IdOfContragent
    left join CF_Transactions tr1_ on this_.IdOfTransaction=tr1_.IdOfTransaction
    left join CF_Clients cl2_ on tr1_.IdOfClient=cl2_.IdOfClient
    left JOIN CF_Orgs org on cl2_.IdOfOrg=org.IdOfOrg
    where c3_.ClassId=1
    group by org.IdOfOrg, c3_.IdOfContragent
    order by org.IdOfOrg, c3_.IdOfContragent

    but iReport throws exception
    Error filling print... Error incrementing crosstab dataset
    net.sf.jasperreports.engine.JRRuntimeException: Error incrementing crosstab dataset      at net.sf.jasperreports.engine.fill.JRFillCrosstab$JRFillCrosstabDataset.customIncrement(JRFillCrosstab.java:806)      at net.sf.jasperreports.engine.fill.JRFillElementDataset.increment(JRFillElementDataset.java:177)      at net.sf.jasperreports.engine.fill.JRCalculator.calculateVariables(JRCalculator.java:164)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:758)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:301)      at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:148)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)      at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:822)      at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:61)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:446)      at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:276)      at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:745)      at com.jaspersoft.ireport.designer.compiler.IReportCompiler.run(IReportCompiler.java:891)      at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)      at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)  Caused by: net.sf.jasperreports.engine.JRException: Crosstab data has already been processed.      at net.sf.jasperreports.crosstabs.fill.calculation.BucketingService.addData(BucketingService.java:311)      at net.sf.jasperreports.engine.fill.JRFillCrosstab$JRFillCrosstabDataset.customIncrement(JRFillCrosstab.java:802)      ... 14 more 

    ReplyDelete
    Replies
    1. Hi Дамир Кадыров
      Please note nested exception is Crosstab data has already been processed. it means that jasper is called twice to rerun the dataset while it has already done with it.
      it look like that you added a crosstab in the detail section of the main report. check my step 7/8.

      Hope it helps

      Delete
  7. Hi....thanks soo much for the tutorial!!! I went through the steps, and my report runs fine. The problem is that only 1 row of data is showing up. Like in your report all of your options(task 1-3) in the TASK column is showing up. My column consist of MONTHS, but only 1 month is showing up as a column. I have checked the Print Repeated Values option, but no luck. Is there an option that should be set?

    ReplyDelete
  8. Thank you very much indeed, you saved my time.

    ReplyDelete
  9. Friends i need the out put as

    Date | Heading 1 | Heading 2 | Heading 3 |
    | | Sub1 | Sub2 | |

    I am using cross tab and taking two fielfs in column group 1 is Heading 2 is Sub heading. 1st heading dont have sub heading so it should come in middle of the text box and 2nd column is having sub heading so it should come as shown above and third dont have sub heading it should come in middle of the text.

    Please give me solution
    Thanks

    ReplyDelete
  10. Hi Rajendra,

    If I want to keep the first column of the rows hidden, that is, "SubTask1,SubTask2..." hidden and display the remaining output as it is, how should I go about it?

    ReplyDelete
  11. in cross tab u remove subtask1... and close the field

    ReplyDelete
  12. Ok,that's helpful Rajendra..:)
    Now if i want to see the row wise sum as Total then how can you do that??
    Like,It'll show 0.25 in Next to Scheduled column as Total for the first SUBTASK1..Ex-(Actual (0.25)+Scheduled(null) )
    Then 0.85 for SUBTASK2,same for the rest..
    I'm stuck in a report for such calculation..

    ReplyDelete
    Replies
    1. Sorry for late reply, sum is possible. i can't help you quickly as i need to do setup and test to reply properly as post is quite old now and I have no backup.
      if I find time i will try to reply you...but can't promisse.

      Delete
    2. waiting for your reply boss...:)

      Delete
    3. in report i am already showing sub grouping/sum of task scheduled wise.... i think i did not get your requirement...
      if you need total of actual and scheduled together for each sub task then in column grouping keep only one grouping of task column that can give you row wise total of all tasks...
      if i am wrong kindly let me know your requirement again.

      Delete
    4. Please check the following link...
      https://plus.google.com/u/0/+MakReza/posts/6BsfyAYidEF

      Delete
    5. Hi Reza,

      I saw your pic, I hope you need only total of "task1" irrespective of series (means total of actual and scheduled)
      I could achieved it. but it hard to explain...
      Well i try it below hope you get it...
      (I am using ireport designer tool)

      1. in your report you have cross tab go to cross tab report
      2. in report left side you will see report inspector in that go to summary>>measures here right click and you will see context menu add new measure.
      3. name : measure1
      measure class: Bigdecimal
      value expression: ( ($F{TASK}.equals("TASK1")) ?( ( null != $F{PERCENT})? $F{PERCENT}:0 ) : 0 )
      calculaation: Sum
      4. drag and drop new text field in Total Series details part
      so your text field will appear in "Details/Series" in report inspector
      5. go to textfield expression click (...) popup will appear with text field expression.
      6. there you will find the new variable created as "measure1 (total by series)" select it so that in expression editor field appear $V{measure1_SERIES_ALL}
      click ok
      and run report.

      I could see report as expected by you hope you also get it,
      you need to try changing field expression as explained in step 3 and 6 in your real report as per your requirement.
      here in step 3 i created a variable where i put a condition to consider the field for sum only if task name is task1 and not null

      Good Luck!!

      Delete
    6. Thank u so much for your reply bro..I will apply it and let you know whether it works or not..:)

      Delete
  13. i tried the same but it is showing only one value

    ReplyDelete
  14. Hello Rajendra Sir,

    We are creating software which will help us to generate Marksheet of the students of our college i am using JasperSoft for this...
    My Query is i have column name Subject-Code in which subject codes are stored row-wise , what i want is when i generate the report the the row wise data should be displayed column wise in the report
    can you plz help..

    ReplyDelete
    Replies
    1. Hi Sandhya,

      Please follow my steps as given in post in your case you need to give query
      select subject-code from xyz table (or if you have any other coplex query ) simply follow next steps and you will get your report done

      hint you have to fire select query normally but while designing report in jasper your row you have to select as column and rest taken care by jasper report

      good luck
      Rajendra

      Delete
  15. hi in cross tab how to add one after another using single cross tab

    ReplyDelete