Skip to main content
Inspiring
November 12, 2006
Question

Technique for adding query fields to a report that cant be calculated from the tables on the report's query

  • November 12, 2006
  • 1 reply
  • 377 views
Perhaps this is useful to someone using Report Builder.

I have a report that has a couple of fields that are not in the database tables that drive the report but instead are calculated from a range of inputs for each record in the query that drives the report. The calculations to get the values for these fields cannot be done in the Report Builder since the input values are not part of the query that feeds it. The data must be in the query, but the values cant be retrieved using SQL alone.

This is what I did. Lets use the example of a field called "FactorX".

In the Report Builder menu click Report / Fields and Parameters / Add query field. Enter the field name "FactorX" and set the data type as appropriate (double, in my case) then click OK. FactorX will now be added to the list of query fields. Drag it onto your report layout and format it as you would for any other field.

Let's say you have a CF app that supplies the query for the report at run-time like:

<CFQUERY name="myQuery" datasource="someDSN">
Select * From Customers
</CFQUERY>

For each customer record we want to add a column that has the calculated value FactorX.

1. Add a new column to the resulting query recordset:

<cfset NewFieldArray = ArrayNew(1)> <!--- We need a blank array to satisfy the QueryAddColumn function --->

<cfset tmp = QueryAddColumn(myQuery, "FactorX", "Double", NewFieldArray)> <!--- Make sure your datatype matches the one in your report layout --->

2. Now calculate the value of FactorX for each customer and add it to the query recordset:

<cfloop query="myQuery">
<cfscript>
facX = CalcFactorXFor(custID); <!--- A function or method that does the calc based on the current custID --->
tmp = QuerySetCell(myQuery, "FactorX", facX, CurrentRow);
</cfscript>
</cfloop>

Finally, pass the query "myQuery" to the CFReport tag as usual. Obviously you may add as many of these fields as you need.

Cheers,
Murray

This topic has been closed for replies.

1 reply

MurrahAuthor
Inspiring
November 12, 2006
As a follow up to this.

It is also possible to call your own UDFs from the report itself as long as the UDF is on the same page as the CFReport tag.

ie You can insert a field into the Report Builder layout and in the expression builder enter (for example) myCalc(). As long as the myCalc function is available on the page that calls the report, the function will be called. Of course, you can pass parameters to the function as well.

I dont know whether it is faster to add the data to the query before the report is generated, as in the previous post, or call the function when the report is generated. It might be that depending upon the results of the fields added to the query, you might not run the report at all. So that would be one advantage to sorting out the data before you call cfreport.

Cheers,
Murray