Skip navigation
Currently Being Moderated

Problem with Group By on Multiple Columns

Jun 8, 2006 5:30 PM

I have a table with 5 columns:
id | provider | referrer | pt_count | address1

I'm trying to create a report page which groups by address1, creates a sum total of the pt_count for all address1 records, then displays a list of all referrers, then a list of all providers associated with address1.

I am able to group the address1 results and get the sum total of the pt_count for each address1 with the following SELECT statement:

<CFQUERY name="tuccreport" datasource="#myDB#" dbtype="ODBC">
SELECT DISTINCT address1, SUM(pt_count) AS Total_Patients, referrer, provider
FROM tucc_final
GROUP BY address1
ORDER BY Total_Patients desc
</cfquery>

When I output the results:
<CFSET myCounter="1">
<CFOUTPUT query="tuccreport" GROUP="address1">
<tr>
<td id="data0" valign="top">#myCounter#</td>
<td id="data1" valign="top">#address1#</td>
<td id="data2" valign="top">#Total_Patients#</td>
<td id="data3" valign="top"></td>
<td id="data4" valign="top"></td>
</tr>
<CFSET myCounter=(#myCounter#+1)>
</CFOUTPUT>

I get a table with a counter, then the first two fields just as I want them: An address, then a sum total of the pt_count field for all instances of that address.

Now all I need to do is add the groups of referrer and providers, and I'm done. But as soon as I change the query to:

<CFQUERY name="tuccreport" datasource="#myDB#" dbtype="ODBC">
SELECT DISTINCT address1, SUM(pt_count) AS Total_Patients, referrer, provider
FROM tucc_final
GROUP BY address1, referrer
ORDER BY Total_Patients desc
</cfquery>

The results start getting broken down. My address group is still getting some sort of partial total, but it is broken down so it appears spread out over multiple rows in the report table.

Same thing happens if I add "provider" to the Group By, it breaks the results down even further. There is still grouping going on based on Address1, but the Address1 group appears to be getting broken into chunks somehow.

Any ideas?

Thanks,
max
 
Replies
  • Currently Being Moderated
    Jun 8, 2006 8:12 PM   in reply to Max_Donnelly
    First you don't need the distinct keyword in your select clause when you are selecting an aggregate (count, sum, etc). You will get distinct records.

    Next, when you have an aggregate in your select clause, you have to group by every non-aggregate field in the select clause. You have already figured that out it seems.

    To actually get what you want, query of queries seems like the best approach. Query your db for the raw data and then do a q of q to get the sum for each address. Then work with both queries to output your data as you see fit.
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2006 9:20 PM   in reply to Max_Donnelly
    <CFSET myCounter="1">
    <CFOUTPUT query="tuccreport">
    <CFQUERY name="referquery" dbtype="query">
    SELECT DISTINCT referrer
    FROM tuccreport
    WHERE address1='#address1#'
    </cfquery>
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 8, 2006 11:44 PM   in reply to Max_Donnelly
    Just going to throw my 2cents in here.

    <CFQUERY name="tuccreport" datasource="#myDB#" dbtype="ODBC">
    SELECT referrer, provider, st.address1, st.Total_Patients
    FROM tucc_final Inner Join (address1, SUM(pt_count) AS Total_Patients From tucc_final GROUP BY address1) As st
    On tucc_final.address1 = st.address1
    ORDER BY st.Total_Patients desc
    </cfquery>

    Then use the group attribute of cfoutput when outputting results.

    Ken
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2006 12:34 PM   in reply to Max_Donnelly
    You are missing the word 'select' in your subquery.
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 9, 2006 12:35 PM   in reply to Max_Donnelly
    Hi Max,

    I think he missed just one statement:

    SELECT referrer, provider, st.address1, st.Total_Patients
    FROM tucc_final Inner Join ( SELECT address1, SUM(pt_count) AS Total_Patients From tucc_final GROUP BY address1) As st
    On tucc_final.address1 = st.address1
    ORDER BY st.Total_Patients desc

    HTH,
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 10, 2006 6:29 PM   in reply to Max_Donnelly
    First, sorry about missing the select keyword.

    As I don't know the data, it's a bit hard, but
    I assume that a provider can have more than one address and referer, so I think all you need to do is move the goups around

    First group by the provider, then the referer, then the address

    Ken
     
    |
    Mark as:
  • Currently Being Moderated
    Jun 12, 2006 9:27 AM   in reply to The ScareCrow
    I think it would help to understand the functionality of CFOUTPUT by group.

    Take this sample data:
    company - product
    Joe's Flowers - begonias
    Joe's Flowers - asters
    Joe's Flowers - tulips
    Mary's Florist Service - gardenias
    Mary's Florist Service - daffodils
    Mary's Florist Service - pansies


    You want output to read:

    Joe's Flowers
    - begonias
    - asters
    - tulips

    Mary's Florist Service
    - gardenias
    - daffodils
    - pansies

    To use CFOUTPUT with the group:

    <CFOUTPUT group="company">
    #company#
    <CFOUTPUT>
    - #product#
    </CFOUTPUT>
    </CFOUTPUT>

    The inner CFOUTPUT section runs as long as the outer value (company) does not change. This is why it's important to order your query results properly. "GROUP" is sort of a misnomer in that it has nothing to do with the SQL GROUP BY statement; it's all about how you ORDER the results.

    HTH,
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points