In this blog, you will learn about how to use Apply before Aggregate function. Basically this can be used to specify the sequence in which we would like to apply the dbfunctions along with aggregate functions i.e. whether first aggregate function should get applied or whether first DBfunctions should get applied.
Let us understand with an example below
Example: Following are the Aggregate function which can be applied to the column:
- Sum
- Distinct
- Count
- Max/Min
- Avg
Let us say we apply one aggregate function like Average (Avg) on the travel_cost column which will be creating a SQL like avg (trave_cost).
Now in case your data is in decimals and you would like to round it off to 2 decimals, then user can apply DBfunction like ceiling.
So immediaelty if DBfunction is applied it would generate SQL query like below
avg(CEILING (travel_cost))
Whereas the ideal expectations would be that inside avg should be applied and outside ceiling should be applied. For that case a user can click on “Apply Before Aggregate”
When this is used the dbfunction will get applied before aggregate function and it will generate like below CEILING (avg(travel_cost))
Please follow these steps below:
Step1: We are selecting one field which will be an Integer like travel_cost, this column automatically comes up with a sum aggregate function.
Step2: Now here we are doing uncheck this SUM aggregate function and applying only Apply Before Aggregate.
Step3: Now we are selecting the Functions option to create some DB function on travel_cost,
For eg We want to use the value 12.333 and this value should Return the number rounded up to the nearest integer like 123 using this CEILING (travel_cost), please save this function and generate the report.
Step4: Then here you have to apply the Aggregate function like avg please generate the report and see what SQL query is generating,
Step5: Here this Aggregate function and DB function is applying perfectly CEILING (avg(travel_cost)).
In the report, you will see the output is coming as per your expectation.
Thank You,
Helical Insight