SQL is powerful! Use it!


I'll admit I tend to use CF for all my coding but sometimes using SQL is by far and away easier to accomplish your goal as well as just a hell of a lot more quicker. I was struggling with retuning a total number of exercise minutes for a project that I'm working on. I tried double looping, adding variables together all kinds of stuff. Sometimes if you just keep it simple its so much better. 
I needed to track the total number of minutes a team was exercising. Everything is in multiple tables so that was easy part, joining the tables to get the results that I wanted. Great but I needed so the total not just each individual entry.
Like I stated before I tried everything Cf then it hit me I did a query of queries and did a SQL Sum on the column that contains the mins that I wanted to add. 
First I looped all my teams (because I have multipe teams) inside that loop I did a query of queries using SUM on my minutes column As totalMin when I out put the result I got the total of the different teams. 

<cfloop query="selectAllTeams" >
<cfquery dbtype="query" name="qoq" >
SELECT SUM (min_exer) As totalMin
FROM totalMin
WHERE totalMin.name_Teams = <cfqueryparam cfsqltype="cf_sql_varchar" value="#selectAllTeams.name_teams#">
</cfquery>
count =<cfoutput>#qoq.totalMin#</cfoutput>
</cfloop>


Obviously I'm giong to need to do the if statements if the result is blank but hey there you go SQL saves the day! 

Query Results