Multiple Reports with One SQL Statement

How to dynamically write reports ?On the Fly?

This is a simple way of querying your database for specific instances and only having to write one SQL query that does it all. This example is something I wrote for an Inventory Management System at my job.

Here is the Results Page with the SQL Statement

<cfquery name="viewreports" datasource="#application.dsn#">
    Select columnname1, columnname2?.
    from table
    where 1=1
    #urldecode(preservesinglequotes(url.whatever))# <!--- (url.whatever means you can name this url.whatever you want) --->
</cfquery>

<table>
    <th>
Service Tag</th>
    <th>
Machine Name</th>
    <th>
Owner</th>
    <th>
Make</th>
    <th>
Mode</th>
    <th>
Speed</th>
    <th>
Memory</th>

    <cfoutput query="viewreports"
<!--- (This basically checks the data and if the field is blank, returns a ?not found? message in the report or returns the data ?trimmed? with no spaces in front (just in case there is) --->
    <tr>
        <td>#iif(len(trim(servicetag)) eq 0, de("no service tag"), de(trim(servicetag)))#</td>
        <td>
#iif(len(trim(machinename)) eq 0, de("no machine name"), de(trim(machinename)))#</td>
        <td>
#iif(len(trim(owner)) eq 0, de("no owner name"), de(trim(owner)))#</td>
        <td>
#iif(len(trim(make)) eq 0, de("no make"), de(trim(make)))#</td>
        <td>
#iif(len(trim(model)) eq 0, de("no model"), de(trim(model)))#</td>
        <td>
#iif(len(trim(processorspeed)) eq 0, de("no speed"), de(trim(processorspeed)))#</td>
        <td>
#iif(len(trim(memory)) eq 0, de("no memory"), de(trim(memory)))#</td>
    </tr>
   
</cfoutput>
</table>



This basically is a text page with URL?s leading to the necessary reports. 

<cfoutput>
<table width="40%" border="2" bordercolor="E8E8E8">
    <tr bgcolor=
"ffe4b5">
        <td>
All Reports</td>
    </tr>
    <tr>
        <td><a href=
"viewreports.cfm?whatever=#urlencodedformat("and column1='Desktop'")#">All Desktop Users</a></td>
    <tr>
        <td><a href=
"viewreports.cfm?dsql=#urlencodedformat("and column2='Dell' and column1='Desktop'")#">All Dell Desktop Users</a></td>
<!--- (example of multiple selections ? it searches for Dell and Desktop and returns all users who have a Dell Desktop) --->
    </tr>
</table>
</cfoutput>


Everyone has had their bosses say ?I need a report with this, this, and this and I need it now?. Using this code, you can simply copy and paste the code, change the data that you are looking for and then have a report ? without having to write multiple SQL statements.

All ColdFusion Tutorials By Author: Jennifer Davis
  • Multiple Reports with One SQL Statement
    Your boss wants multiple reports from your database and he wanted them 5 mintues ago. This is a quick way to write one SQL statement but be able to query your database multiple times while specifying what values you want it to return.
    Author: Jennifer Davis
    Views: 17,121
    Posted Date: Thursday, March 18, 2004
  • Querying a Query
    This will show you how to query a query. It also demonstrates how to use the datediff function and how to find an average.
    Author: Jennifer Davis
    Views: 26,613
    Posted Date: Thursday, May 6, 2004
  • Choosing the Number of Columns to Display Your Data
    This tutorial shows how to display queried data in a determined amount of columns instead of the default one column
    Author: Jennifer Davis
    Views: 15,676
    Posted Date: Friday, January 14, 2005
  • Dynamically Add Rows To a Form
    Adding lines to a form dynamically using javascript and CF. User enters data, clicks Add Another, the page reloads. It keeps the data from line 1 and adds another blank row that is ready for data entry.
    Author: Jennifer Davis
    Views: 28,271
    Posted Date: Monday, February 21, 2005
Download the EasyCFM.COM Browser Toolbar!