ColdFusion 10 - Using The Group Attribute With CFLoop To Group Query Rows

Posted April 9, 2012 at 10:21 AM by Ben Nadel

Tags: ColdFusion

Before ColdFusion 10, the "group" attribute was only available on the CFOutput tag. This attribute could be used to create pseudo "GROUP BY" behavior when outputting ColdFusion query data. With ColdFusion 10, the "group" attribute can now be used with the CFLoop tag when looping over a query object. In most cases, I prefer executing ColdFusion's query-of-queries to produce this behavior; but, in a small set of use cases (like creating drop-down menus), I can definitely see the "group" attribute making life a lot easier.

NOTE: At the time of this writing, ColdFusion 10 was in public beta.

The "group" attribute works by grouping together adjacent rows with matching column values - the target column is determined by the "group" attribute. Once grouped, you can then loop over the grouped columns within a subsequent CFLoop tag. Unlike the GROUP BY clause in a SQL query, the "group" attribute depends on query that has been pre-sorted on the target column.

To demonstrate this feature, I've constructed a query with Male and Female friends. The query is already sorted such that all Female friends are listed first. I will then output the query in two distinct gender groups using the "group" attribute:

  • <!---
  • Create a query with sibling rows that have matching data. We'll
  • use the GROUP attribute to condence similar rows to a single
  • outer row of our query.
  •  
  • NOTE: We are using the new QueryNew() features of ColdFusion 10
  • to populate the new query.
  • --->
  • <cfset friends = queryNew(
  • "id, name, gender",
  • "cf_sql_integer, cf_sql_varchar, cf_sql_varchar",
  • [
  • [ 1, "Sarah", "Female" ],
  • [ 2, "Tricia", "Female" ],
  • [ 3, "Joanna", "Female" ],
  • [ 4, "Arnold", "Male" ],
  • [ 5, "Vin", "Male" ]
  • ]
  • ) />
  •  
  •  
  • <cfoutput>
  •  
  • You have #friends.recordCount# friends!<br />
  •  
  • <!---
  • Loop over the friends, but output them by adjacent gender
  • (using the GROUP attribute). This way, we can output the
  • Women and then the Men.
  • --->
  • <cfloop query="friends" group="gender">
  •  
  • <br />
  • #friends.gender#:
  • <br />
  •  
  • <!---
  • Since we are in a Grouped query, we need to run an inner-
  • cfloop to output the actual grouped rows. Exclude the
  • NAME attribute since we want this CFLoop tag to connect
  • with the outer, grouped tag.
  • --->
  • <cfloop>
  •  
  • - #friends.name#<br />
  •  
  • </cfloop>
  •  
  • </cfloop>
  •  
  • </cfoutput>

NOTE: I am using the enhancements of QueryNew() in ColdFusion 10 to construct the query object manually using an array of row values.

Notice that when I go to use the first CFLoop tag, I am using the [group="gender"] attribute. This tells ColdFusion to group together adjacent rows with the same "gender" column value. Inside this CFLoop tag, I then have another CFLoop tag that executes the group-specific loop. In order for this CFLoop tag to link to the outer, group-wise CFLoop tag, I have to exclude the Name attribute. If I were to include the Name attribute, this inner loop would execute completely independently.

When we run the above code, we get the following, grouped output:

You have 5 friends!

Female:
- Sarah
- Tricia
- Joanna

Male:
- Arnold
- Vin

As you can see, the sibling rows with the same gender value were all lumped into the same group. While this grouping is very limited in behavior (ex. it doesn't offer group-based properties like RowCount or CurrentRow), it can definitely make simple output algorithms extremely easy.


You Might Also Be Interested In:



Reader Comments

Apr 9, 2012 at 10:28 AM // reply »
3 Comments

This is really cool. I didn't realize the output tag grouped function worked that way. I can really see this being useful. Thanks for the post Ben!


Apr 9, 2012 at 10:29 AM // reply »
10 Comments

Wow, I've been waiting basically a DECADE for this attribute! FINALLY!


Apr 9, 2012 at 10:40 AM // reply »
11,238 Comments

@Ryan V,

No problem my man!

@Ryan J,

Ha ha, word up! It has been a super long time in the making :)


Apr 9, 2012 at 5:57 PM // reply »
6 Comments

The way you're using this feature has been available in ColdFusion for years. In current versions of ColdFusion you can perform this exact action using cfoutput tags instead of cfloop. This entails adding more cfoutput tags than maybe you'd like throughout the page, but the way you're showing this feature isn't really a new feature.

Love the site Ben, just thought I'd point this out in case anyone is unaware of this option in current version of ColdFusion.


Apr 9, 2012 at 6:34 PM // reply »
5 Comments

Still wish there was a GroupRecordCount


Jan 23, 2013 at 3:44 PM // reply »
1 Comments

I think the real power of this can be seen nesting cfoutput using group with a cfloop query using its own group nested inside. I have ran into this a time or two and now it looks like a clean solution to my woes has evolved in CF10.....Thanks for the insight Ben


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
May 20, 2013 at 11:45 AM
Using jQuery's Animate() Step Callback Function To Create Custom Animations
This is really useful. I found out that you don't actually have to use a dummy css property (surprisingly). To animate a property in a linear-gradient for instance I did this this.css('someLinearGra ... read »
May 20, 2013 at 10:51 AM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Josh, Oh snap! You're totally right! I'm not sure I've ever tried that. I did know that you can call a number of other array-methods on ColdFusion query columns: http://www.bennadel.com/blog/167 ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools