Multiple subqueries to create a Report
I can't seem to find the appropriate solution for a report. I have Joined two tables together to get all the information I need, but now I have to group data by state and group by three other variables - New, Retained, Cancelled. Then loop through the amount of states that are in the DB. Assuming I have UT and WA
The report should look something like this:
| State: UT | Number | Premiums |
|---|---|---|
| New Policies | 3 | $74.97 |
| Retained Policies | 2 | $13.98 |
| Cancelled Policies | 1 | $5.99 |
| Net Policies | 4 | $82.96 |
| State: WA | Number | Premiums |
|---|---|---|
| New Policies | 3 | $74.97 |
| Retained Policies | 2 | $13.98 |
| Cancelled Policies | 1 | $5.99 |
| Net Policies | 4 | $82.96 |
Here is my sort of working Code:
<CFQUERY NAME="ActiveCustomers" DATASOURCE="#db#">
SELECT * FROM tbl_customers
INNER JOIN tbl_plans ON tbl_plans.ProductID = tbl_customers.ProductID
WHERE Status = "Active" AND Month(DateSignup) < "#Form.Month#"
ORDER BY DateSignup asc;
</CFQUERY>
<cfset ActiveCustomerTotal = 0>
<cfoutput query="ActiveCustomers">
<cfset ActiveCustomerTotal = #ActiveCustomerTotal# + #ActiveCustomers.ProductPrice#>
</cfoutput>
<CFQUERY NAME="NewCustomers" DATASOURCE="#db#">
SELECT * FROM tbl_customers
INNER JOIN tbl_plans ON tbl_plans.ProductID = tbl_customers.ProductID
WHERE Status = "Active" AND Month(DateSignup) = "#Form.Month#"
ORDER BY DateSignup asc;
</CFQUERY>
<cfset NewCustomerTotal = 0>
<cfoutput query="NewCustomers">
<cfset NewCustomerTotal = #NewCustomerTotal# + #NewCustomers.ProductPrice#>
</cfoutput>
<CFQUERY NAME="CancelledCustomers" DATASOURCE="#db#">
SELECT c.Status, c.ProductID,(SELECT SUM(p.ProductPrice) FROM tbl_plans p WHERE p.ProductID = c.ProductID) AS ItemSum
FROM tbl_customers c
WHERE c.Status = "Cancelled" AND Month(c.Cancelled) = "#Form.Month#"
ORDER BY Cancelled asc;
</CFQUERY>
<cfset CancelledCustTotal = 0>
<cfoutput query="CancelledCustomers">
<cfset CancelledCustTotal = #CancelledCustTotal# + #ItemSum#>
</cfoutput>
I have looked everywhere for help.
