Jump to content
Changes to the Jaspersoft community edition download ×

Adhoc Calculated Field, Calculate AFTER Aggregation

Go to solution Solved by bobtins,

Recommended Posts

Is it possible to create a field that calculates based off the sums of two other fields (i.e., after aggregation)?

I have retail prices and discounted prices, and want to create a weighted average discount, which is the sum of all discounted prices over the sum of all retail prices. This is a fundamentally different # than taking the discount rate on a row by row basis and then averaging those numbers. I can figure out how to do the latter (unweighted) calculation in domain designer, and how to calculate something simple like dividing the sum of discounted prices by the total for the column in the adhoc view creator, but not to do a division of two fields post aggregation.


Example data:


.092 weighted (this row discount/retail)
.233 unweighted (average of column)


Link to comment
Share on other sites

  • Replies 8
  • Created
  • Last Reply

Top Posters In This Topic

  • Solution

Thanks, this is a good example of something we should be able to support in the product. We are implementing an expansion of the calculated fields capabilities in Ad Hoc for next release, and one thing I would like to enhance is the way we handle summary calculations. Currently, we just allow the user to specify a function, but for cases like this, the user should be allowed to have full control over how to calculate the summary.

A few releases back, we did make a change so that the sum of a calculated field "a/b" would be "sum(a)/sum(b)". The formula that you want for the summary is actually "1 - sum(discount)/sum(retail)".

I put your sample data into a database and tried it out. What I found was that the summary for "discount/retail" comes out as "sum(discount)/sum(retail)", but not summary for "1 - discount/retail" is really "sum(1 - discount/retail)", which is not correct.

Thanks again for bringing this up. Often we are trying to strike a balance between ease of use and power in designing the user interface. In cases like this, users have a good idea of what to do, and we should figure out how to get out of the way and let them do it!


Link to comment
Share on other sites

Interesting... did not realize that it differentiated and did something else for a/b than for 1-(a/b). Using that info, I was able to get around this by creating an intermediary calculated field for retail - discounted (call it dscntdol), then divide dscntdol by retail. I then get the answer I seek, calculated at the last possible moment.
Link to comment
Share on other sites

  • 5 months later...
There is a button in the ad hoc editor that can be enabled (it is disabled by default) (you must be the super user to enable it) that allows you to view the query that is being sent to the database. In the JasperReports Server 5.5 Admin Guide it's described in section 7.8 Configuring Ad Hoc, 7.8.1 Ad Hoc Query Settings. Not sure where it is in the 4.5 guide, but I'd image it is similar.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...