Let SQL Do The Data Work
Many come to ColdFusion from HTML development and find out that CF is great for putting data into their beautiful web pages. The natural tendency is to select all the database and dump it into the page. As the data grows, the new developer will pick up some SQL skills to filter the list down and even do some grouping of data and then output it nicely on the page. ColdFusion does this well with its CFOUTPUT attribute GROUP. This is all just wonderful.
Then a requirement comes along. Our aspiring developer then figures out he can use CFLOOPS and CFIF statements to control the output with even more but he notices his code is getting messy and pages are loading more slowly. He is sensing that he might be asking ColdFusion to do things the database server is better suited for. He is right.
A good practice in web application development is to separate the data manipulation from the data presentation. Database servers are designed to crunch data efficiently and have functions built in to do most if not all of the data manipulation. A developer should strive to have the data all crunched and manipulated before it is presented back to ColdFusion for presentation.
I was recently asked by someone online how they could get maximum value from a group of items with the same id. This would be like finding the top sales rep where the database table (monthly_sales) has the region id (id) and the sales dollars (sales). A developer could query the database for all records for the month and sort by sales dollars and id and then loop through the results and use CFSET and CFIF to out put only the first sales rep in each region. This would be asking the database to send back more data then necessary and would be asking ColdFusion to a little too much crunching.
I thought about this problem for a bit and decided to use SELECT DISTINCT which brought back distinct items if I queried one column. But I was querying two columns so the database would return distinct combinations. I need to get each distinct ID and the maximum dollars so I used DISTINCT along with the MAX function. Here's my SQL that brought back each individual id along with the maximum dollars for each.
SELECT DISTINCT id, MAX(sales) AS sales_dollars FROM monthly_sales GROUP BY id
The moral of the story is that you should get the database to do all the data work and have ColdFusion present the data.
