Grouping Result Output
Before a new level of complexity is introduced, let's review how ColdFusion processes queries.In ColdFusion, data queries are created using the <cfquery> tag. <cfquery> performs a SQL operation and retrieves results if any exist. Results are stored temporarily by ColdFusion and remain only for the duration of the processing of the template that contained the query.The <cfoutput> tag is used to output query results. <cfoutput> takes a query name as an attribute and then loops through all the rows that were retrieved by the query. The code block between <cfoutput> and </cfoutput> is repeated once for each and every row retrieved.All the examples created until now displayed results in a single list or single table.What would you do if you wanted to process the results in subsets? For example, suppose you wanted to list movies by rating. You could change the SQL statement in the <cfquery> to retrieve the rating ID and set the sort order to be RatingID and then by MovieTitle.This would retrieve the data in the correct order, but how would you display it? If you used <cfoutput> as you have until now, every row created by the <cfoutput> block would have to be the same. If one had the rating displayed, all would have to because every row that is processed is processed with the same block of code.Look at Figure 10.11. As you can see, the screen contains nested lists. The top-level list contains the rating IDs, and within each rating ID is a second list containing all the movies with that rating. How would you create an output like this?
Figure 10.11. Grouping lets you display data grouped into logical sets.
[View full size image]

Listing 10.9. ratings1.cfmGrouping Query Output
Figure 10.12). Save Listing 10.10 as ratings2.cfm, and then execute it in your browser.
<!---
Name: ratings1.cfm
Author: Ben Forta (ben@forta.com)
Description: Query output grouping
Created: 12/15/04
--->
<!--- Get movie list from database --->
<cfquery name="movies" datasource="ows">
SELECT MovieTitle, RatingID
FROM Films
ORDER BY RatingID, MovieTitle
</cfquery>
<!--- Creat260 page --->
&l275>
<head>
<title>Orange Whip Studios - Movie List</title>
</head>
<body>
<h2>Movie List</h2>
<!--- Display movie list --->
<ul>
<!--- Loop through ratings --->
<cfoutput query="movies" group="RatingID">
<li>#RatingID#</li>
<ul>
<!--- For each rating, list movies --->
<cfoutput>
<li>#MovieTitle#</li>
</cfoutput>
</ul>
</cfoutput>
</ul>
</body>
</html>
Listing 10.10. ratings2.cfmGrouping Query Output
<!---
Name: ratings2.cfm
Author: Ben Forta (ben@forta.com)
Description: Query output grouping
Created: 12/15/04
--->
<!--- Get movie list from database --->
<cfquery name="movies" datasource="ows">
SELECT MovieTitle, RatingID
FROM Films
ORDER BY RatingID, MovieTitle
</cfquery>
<!--- Creat260 page --->
&l275>
<head>
<title>Orange Whip Studios - Movie List</title>
</head>
<body>
<h2>Movie List</h2>
<!--- Display movie list --->
<table>
<!--- Loop through ratings --->
<cfoutput query="movies" group="RatingID">
<tr valign="top">
<td>Rating #RatingID#</td>
<td>
<!--- For each rating, list movies --->
<cfoutput>
#MovieTitle#<br>
</cfoutput>
</td>
</tr>
</cfoutput>
</table>
</body>
</html>
Figure 10.12. Grouped data can be used in lists, tables, and any other form of data presentation.
RatingID new table row is created containing two cells. The left cell contains the RatingID, and the right cell contains the movies.To do this, the inner <cfoutput> loop is used in that right cell (between the <TD> and </td> tags) so that, for each RatingID listed on the left, all the appropriate movies are listed on the right.