Pregunta

I am trying to run a report on incidents. This code works fine to display it. But I'd like to get the report displaying the most incidents first descending. Adding an order by does not do it. I am also counting the amount of incidents by doing a group count. And thus able to get the percentage. But the display is displaying the incident code in order - but not the actual amount of incidents in order. I'm at bit of loss - can't seem to find solution on Goog or here... Using CF9 and Access...

Displays like this...

Call Type Call Numbers

Dollar Loss Fire 2
Medical CoResponse 11
Vehicle Accident 6
Rubbish or Grass Fire 1
Gas Leak - Natural Gas 5
Alarm - No Fire - Catalogue Alarm 1
Alarm - No Fire - CO Alarm 4
Complaint Investigation 2

I'd like it to display the most first and then decrease like this...

Medical CoResponse 11 Vehicle Accident 6
Gas Leak - Natural Gas 5
Alarm - No Fire - CO Alarm 4
Dollar Loss Fire 2
Complaint Investigation 2
Rubbish or Grass Fire 1
Alarm - No Fire - Catalogue Alarm 1

    <cfquery name="calls" datasource="report">
select * from master
where eventdate like '%#yydate#%'
order by incidentcode
</cfquery>

<cfoutput>
<b>#yydate# #calls.recordcount# Calls</b><br><br>
</cfoutput>

<table cellpadding=8 cellspacing=0 border=1>
<tr bgcolor=b3b3b3>
<td align=center>Call Type</td>
<td align=center>Call Numbers</td>
<td align=center>Percent</td>
</tr>

  <cfoutput query=calls group="incidentcode">
  <cfset groupCount = 0>
  <cfoutput>
  <cfset groupCount = groupCount + 1>
  </cfoutput>
  <cfquery name="code" datasource="report">
  select * from incidentcode
  where id = #incidentcode#
  </cfquery>
<tr>
<td align=center>#code.event#</td>
<td align=center>#groupcount#</td>
<td align=center>
<cfset perc = (#groupcount# / #calls.recordcount#) * 100>
#numberformat(Perc, "_._")# %
</td>
</tr>  
  </cfoutput>
¿Fue útil?

Solución

This isn't complete, but hopefully gets you on the right track. You should be able to write your queries as one query and then output it that way using aggregate totals.

<cfquery name="calls">
SELECT m.incidentcode ic, ic.event event, SUM(m.incdentcode) total
FROM master m
  INNER JOIN incidentcode ic ON m.incidentcode = ic.id
where eventdate like '%#yydate#%'
group by m.incidentcode, event
order by ic
</cfquery>

<cfoutput query="calls">
  <tr>
    <td>#code.event#</td>
    <td>#code.total#</td>
  </tr>
</cfoutput>

The preferred way would be to create one query, but in keeping with your original syntax this should work as well. You may want to double check that sortedStruct is being sorted correctly. It might sort by key name first.

<cfset allCalls = {}>
<cfset totalCount = 0>
<cfoutput query=calls group="incidentcode">
  <cfset groupCount = 0>
  <cfoutput>
    <cfset groupCount++>
  </cfoutput>
  <cfset totalCount+=groupCount>
  <cfquery name="code" datasource="report">
  select * from incidentcode
  where id = <cfqueryparam cf_sql_type="cf_sql_varchar" value="#incidentcode#">
  </cfquery>
  <cfset allCalls[code.event] = groupcount>
</cfoutput>

<cfset sortedStruct = structSort(allCalls,'numeric','desc')><!--- returns an array ordered the way you want --->
<cfoutput>
  <cfloop array="#sortedStruct#" index="event">
    <tr>
      <td>#event#</td>
      <td>#allCalls[event]#</td>
      <td>#numberFormat(allCalls[event] / totalCount * 100,'00.00')#</td>
    </tr>
  </cfloop> 
</cfoutput>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top