When I first started writing reports, I would jump through all different hoops to output grouped data. So, when I wanted to output something like this:
Division A
Ordernum Dollars
1 50
4 125
Division B
Ordernum Dollars
5 50
7 125
I would first do a query to get all my data for all divisions, then I would do a Query of Query to get all distinct divisions, loop through the divisions for my report breaks, and do another QoQ to get the data related to that division. This is FAR easier to do with a grouped output, I will show you how.First off, you need to structure your query to sort by your groupings. For my example above, the query would look something like this:<br /><br />SELECT<br /> division,<br /> orderNumber,<br /> revenue<br />FROM<br /> myTable<br />ORDER BY<br /> division, orderNumber<br /><br />
Nothing too fancy there, the key is that any groupings you do need to be (in the proper order they will be grouped) specified in your ORDER BY clause.
Now to generate your output:<br /><br /><br />
#Division#
| Order Num | Dollars |
| #ordernum# | #dollarFormat(revenue)# |
So what this will do, your 1st tag says to output your data, but it will only output contained code for each distinct #division# it encounters. It will then loop through all of the records with a matching division inside the inner-cfoutput to display individual table rows for each record. You can nest these as deeply as you wish, so you could have a company group, then a division group, then a unit grouping, etc. Just add a inner <cfoutput group="#myOtherGroupedColumn#">
If you combine this with the use of <thead> and <tbody>, you can produce some very slick reports with headers and footers on each page (when they print) with a minimal effort.








#1 by Steve Bryant - March 6, 2008 at 10:19 AM