Question

Just wondering, given a query and output like so:

<cfoutput query="someItems" group="someColumnName">

     ... doing some stuff here ..

    <cfoutput> doing stuff with some sub items </cfoutput>


</cfoutput>

if there's a way to change the order of elements in the 'inner' cfoutput ? Can the query be both grouped and sorted by?

Was it helpful?

Solution

You will need to add ORDER BY clauses in your query for this to work, but you can nest cfoutput tags that use the group attribute.

<cfoutput query="someItems" group="someColumnName">

     ... doing some stuff here ..

    <cfoutput group="someOtherColumnName> doing stuff with some sub items </cfoutput>


</cfoutput>

This assumes that in your query you have something that looks like:

ORDER BY someColumnName, someOtherColumnName

Keep in mind that the group attribute of cfquery is not the same as the GROUP BY clause in a SQL statement. You can use the group attribute of cfoutput for ANY column that is in the ORDER BY clause in your query.

OTHER TIPS

One solution is to restructure your code to use the query-of-queries approach. Here is a good example of doing so:

http://www.bennadel.com/blog/2211-ColdFusion-Query-Of-Queries-vs-The-Group-Attribute-In-CFOutput.htm

Basically, you pull out all the data you care about in one master query (probably the query you have already written). You add a second query (against your first query, not against the database) that does the group by and aggregation of data that you need at the top level loop. Inside the loop driven by your second query, you use the row data in the group as a parameter to yet another query (against your first query again, not against the database) to pull out all the data relating to the current row ordered however you desire.

This idea of querying your query seems odd at first, but I have not had performance problems with it and it gives you a lot of flexibility to do what you want in your inner loop. Good luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top