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
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