ken.auenson.com

May 27, 2005

Grouping Queries

Filed under: ColdFusion — Ken @ 1:45 am

Cold Fusion has a great built in functionality to automatically group query results. This is a very powerful feature that I use all the time.
I am not very good at explaining this kind of thing in words,
so lets look at an example…

< cfquery name       = "sqlRetrieveResults"          datasource = "#Request.dsn#"          >          select distinct State,                      City,                      Emp_F_Name,                      Emp_L_Name,                      Emp_Phone,                      Emp_Email                   from Employee_Records                   order by    State,                      City,                      Emp_L_Name       cfquery>    < cfoutput query="sqlRetrieveResults" group="State" >           < h2>#sqlRetrieveResults.State#h2>                 < cfoutput group="City" >                   < h4>#sqlRetrieveResults.City#h4>                   < table border="1" >              < tr>                  < th>First Nameth>                  < th>Last Nameth>                  < th>Phone Numberth>                  < th>Email Addressth>              tr>              < cfoutput>                                   < tr>                  < td> #sqlRetrieveResults.Emp_F_Name# td>                  < td> #sqlRetrieveResults.Emp_L_Name# td>                  < td> #sqlRetrieveResults.Emp_Phone#  td>                  < td> #sqlRetrieveResults.Emp_Email#  td>                  tr>             cfoutput>                       table>              cfoutput>            cfoutput>   The code listed above will generate output that looks something like this: 

STATE1

City1

First Name Last Name Phone Number Email Address
Bob Stevens (123) 456-9876 bob@bobs-email.com
Dave Stsadfs (555) 555-1234 dave@hotmail.com

City5

First Name Last Name Phone Number Email Address
abc def (123) 555-3214 abc@def.com

STATE2

City23

First Name Last Name Phone Number Email Address
another guy (123) 555-3212 email@someplace.com

Hope this helps! 

Leave a Reply