Skip to main content
Inspiring
Nov 29, 2011 5:22 PM UTC
Question

cfoutput group by

Hi, my report is similar to the table below.  I need to to get the total of each state but my query didn't work, can you please help !!

StateCost
TX125
TX500
Totaltotal of TX
MO100
MO215
Totaltotal of Mo
CA500
Totaltotal of CA

<cfoutput query="getall">

     <cfset subtotal = 0 />

     <tr>

          <td>#state#</td>

          <td>#amount</td>

     </tr>

     <tr>

          <cfoutput group ="sate">

               <cfset subtotal = subtotal + amount />

          </cfoutput>

          <td>#subtotal#</td>

     </tr>

</cfoutput>

thanks

    This topic is closed to new replies. Start a new post to keep the conversation going.

    1 reply

    BreakawayPaul
    Inspiring
    Nov 30, 2011 3:25 PM UTC

    Try this:

    <cfparam name="total_cost" default="0">

    <cfquery name="qname" datasource="dsn">

    SELECT state

         , amount

    FROM tablename

    ORDER BY state

    </cfquery>

    <table border="1">

    <cfoutput query="qname" group="state">

       <cfoutput>

       <tr><td>#state#</td><td>#amount#</td></tr>

       <cfset total_cost = total_cost + amount>

       </cfoutput>

    <tr><td>Total #state#</td><td>#total_cost#</td></tr>

    <cfset total_cost = 0>

    </cfoutput>

    </table>

    You needed a second (blank) cfoutput nested within the first, with the group attrib on the first because the first one just loops through the states.  The second one loops through the amounts within each state.

    Translate