Querying a Query

<!--- This will show you how to query a query --->
<!--- What this page does is query my db for all tickets entered between
2 dates and then it takes those tickets and finds the ones that have the specific
value of "closed" and then takes the time between those dates for each ticket and calculates
the avg time it took for someone to close the ticket --->

<!--- This is where you select all records from date1 thru date2 --->

<cfquery name="received" datasource="database">
    select RecvdDate, CallStatus
    from database
    where recvddate between <cfqueryparam value="2003-05-01">
      
and <cfqueryparam value="2004-05-01">
</cfquery>

<!--- This is where you query the above query and take the data that is returned from it and
Query it again with another instance --->

<CFQUERY NAME="closed" dbtype="query">
    select RecvdDate, CallStatus
    from received
    where callstatus = 'Closed'
</CFQUERY>

<!--- These two queries basically find the number of tickets opened in a 1yr period
And then finds how many of the tickets were closed during that time --->

<!--- This is where you find the average time it took to close a ticket --->

<cfset diffcount = 0>
<cfoutput query=
"closed">
    <!--- diffcount is eq to 0 + the the # of 'days' between the two dates --->
    <cfset diffcount = diffcount + dateDiff('d', recvddate, closeddate)>
</cfoutput>
<cfset dateavg = diffcount/closed.recordcount>


<!--- This is the output --->

<table>
    <tr>
        <th>
Between 05/01/2003-05/01/2004</th>
    </tr>
    <tr>
        <td>
No. of Tickets Opened:</td>
       
<!--- No. of records returned from 1st query --->
        <td>
<cfoutput>#received.recordcount#</cfoutput></td>
    </tr>
    <tr>
        <td>
No. of Tickets Closed:</td>
       
<!--- No. of records returned from 2nd query --->
        <td>
<cfoutput>#closed.recordcount#</cfoutput></td>
    </tr>
    <tr>
       
<!--- This is where you find the average - use the round statement to make your data
                look cleaner. It returns #.## instead of #.########## --->

        <td>
Avg. Turn-Around:</td>
        <td>
<cfoutput>#Evaluate(round(dateavg*100)/100)#</cfoutput> Days</td>
    </tr>
</table>

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,614
    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!