I'm using My Sql 5.7 and CF11. I want to get the total qTotalPrice & quoteKeyID for each month from the below query. But not a full year. I want to get it from current month to last six month data ( From July to FEB). If there is no data for an particular month, I need to display as an 0 for the particular month.
<cfquery name="thisYearsQuotes" datasource="myDB">
SELECT * , MONTH(qDateTime) as qMonth
FROM Quotes
INNER JOIN Accounts
ON Quotes.aID = Accounts.aID
WHERE Accounts.aID = <cfqueryparam value="#VNAI.aID#" cfsqltype="cf_sql_clob" maxlength="255">
AND Quotes.qDateTime > DATE_ADD(NOW(), INTERVAL -365 DAY)
</cfquery>
For getting each month records I've write a query of query like below.
For getting current month
<cfquery name="SalesTotalThisMonth" dbtype="query">
SELECT SUM(qTotalPrice) as DollarTotal , COUNT(quoteKeyID) as QuoteCount
FROM thisYearsQuotes
WHERE qMonth = #MONTH(NOW())#
</cfquery>
For getting one month back
<cfquery name="SalesTotal1MonthAgo" dbtype="query">
SELECT SUM(qTotalPrice) as DollarTotal , COUNT(quoteKeyID) as QuoteCount
FROM thisYearsQuotes
WHERE qMonth = #MONTH(DateAdd("m",-1,NOW()))#
</cfquery>
For getting two month back,
<cfquery name="SalesTotal2MonthAgo" dbtype="query">
SELECT SUM(qTotalPrice) as DollarTotal , COUNT(quoteKeyID) as QuoteCount
FROM thisYearsQuotes
WHERE qMonth = #MONTH(DateAdd("m",-2,NOW()))#
</cfquery>
etc... Like wise I've to get last six month . So I've wrote a number of query of query for each month.
Is there any possibility to reduce the code?