Skip to main content
Inspiring
June 30, 2009
Question

Query Assistance - Calculating percentage

  • June 30, 2009
  • 2 replies
  • 493 views

Greetings,


I may just be looking at this for way to long and not seeing the proper coding/syntax
required to accomplish what it is I am trying.


I have one query to figure out an overall count for each organization in a database. I
then have a second query to figure out the success rate count to compare to the
overall count per organization. I am running the second query inside the <cfoutput>

of the first query to achieve the desired count/display intended.


The overall and the success counts are displaying as I intend them to. However, I

am unable to get the percentage part to work. Perhaps someone can look at my

code and layout to see where it is I am failing. I have provided the desired look and

my code below for review.


I am certainly open to suggestions on how to do it differently / better, should someone
have an insight on how to accomplish my desired outcome.


CF = CF8

DB = MySQL

Thanks in advance

Leonard B

===== Desired Look =====

Total Attempts
First Attempts
Percent
Organization Name
2018??Org. Name
4033??Org. Name
5144??Org. Name

===== Code Below =====

<cfquery name="rs_attempts" datasource="DSN">

SELECT
Count(airway_management.agency_cert_number) AS agency_count,
airway_management.agency_cert_number,
airway_management.year_of_call,
airway_management.et_attempts,
airway_management.et_attempts_successful,
airway_management.dept_org
FROM
airway_management
WHERE
airway_management.year_of_call = <cfqueryparam cfsqltype="cf_sql_integer" value="2009">
GROUP BY
airway_management.dept_org

</cfquery>

<table width="0" border="1" cellpadding="0" cellspacing="0">
<tbody>
<tr>
    <td><div style="font-family: Arial; font-size: 13px">Total Attempts</div></td>
    <td><div style="font-family: Arial; font-size: 13px">First Attempts</div></td>
    <td><div style="font-family: Arial; font-size: 13px">Percent</div></td>
    <td><div style="font-family: Arial; font-size: 13px">Organization Name</div></td>
</tr>

<cfoutput query="rs_attempts">

<tr>
    <td><div style="font-family: Arial; font-size: 13px">#agency_count#</div></td>
       
        <cfquery name="rs_1_attempts" datasource="DSN">
        SELECT
        agency_cert_number,
        dept_org,
        year_of_call,
        et_attempts,
        et_attempts_successful
        FROM
        airway_management
        WHERE
        year_of_call = <cfqueryparam cfsqltype="cf_sql_integer" value="2009"> AND
        agency_cert_number = <cfqueryparam cfsqltype="cf_sql_varchar" value="#rs_attempts.agency_cert_number#"> AND
        et_attempts = <cfqueryparam cfsqltype="cf_sql_integer" value="1"> AND
        et_attempts_successful = <cfqueryparam cfsqltype="cf_sql_varchar" value="Yes">
        </cfquery>   
       
        <cfset percent = ((#rs_attempts.recordcount# - #rs_1_attempts.recordcount#)*100)>
           
    <td><div style="font-family: Arial; font-size: 13px">#rs_1_attempts.recordcount#</div></td>
    <td><div style="font-family: Arial; font-size: 13px">#NumberFormat(percent,999.00)#</div></td>
    <td><div style="font-family: Arial; font-size: 13px">#dept_org#</div></td>
</tr>

</cfoutput>
</tbody>
</table>

    This topic has been closed for replies.

    2 replies

    Inspiring
    June 30, 2009

    It would probably be simpler to calculate the percentage in your original query.

    SELECT
    Count(airway_management.agency_cert_number) AS agency_count
    , airway_management.agency_cert_number
    , airway_management.year_of_call
    , airway_management.et_attempts
    , airway_management.et_attempts_successful
    , airway_management.dept_org

    , et_attempts_successful / et_attempts * 100 percent_successful

    from etc

    where blah blah blah

    and et_attempts > 0 -- prevent division by zero errors

    Known Participant
    June 30, 2009

    Try replacing this line

    <cfset percent = ((#rs_attempts.recordcount# - #rs_1_attempts.recordcount#)*100)>

    with

    <cfset percent = #(rs_attempts.recordcount - rs_1_attempts.recordcount)*100#>

    Not sure on the brackets though - if not try

    <cfset percentWorking= #rs_attempts.recordcount - rs_1_attempts.recordcount#>

    <cfset percent = #working * 100#>

    Mi-ul