Skip to main content
Known Participant
October 28, 2010
Question

Multiple subqueries to create a Report

  • October 28, 2010
  • 1 reply
  • 802 views

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: UTNumberPremiums
New Policies3$74.97
Retained Policies2$13.98
Cancelled Policies1$5.99
Net Policies4$82.96



State: WANumberPremiums
New Policies3$74.97
Retained Policies2$13.98
Cancelled Policies1$5.99
Net Policies4$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.

    This topic has been closed for replies.

    1 reply

    Inspiring
    October 28, 2010

    Can you post your table structure and the sample data that would be used to generated the result samples you posted?

    bknutzAuthor
    Known Participant
    October 28, 2010

    <!--Query List -->

    <CFQUERY NAME="Customers" DATASOURCE="#db#">

    SELECT c.State, SUM(p.ProductPrice ) AS Price FROM tbl_customers c

    INNER JOIN tbl_plans p ON p.ProductID = c.ProductID

    WHERE  Status = "Active" AND Month(DateSignup) = "10"

    GROUP BY Month(c.DateSignup), c.State

    UNION ALL

    SELECT c.State, SUM(p.ProductPrice )AS Price FROM tbl_customers c

    INNER JOIN tbl_plans p ON p.ProductID = c.ProductID

    WHERE  Status = "Active" AND Month(DateSignup) < "10"

    GROUP BY Month(c.DateSignup), c.State

    UNION ALL

    SELECT c.State, SUM(p.ProductPrice )AS Price FROM tbl_customers c

    INNER JOIN tbl_plans p ON p.ProductID = c.ProductID

    WHERE  Status = "Cancelled" AND Month(Cancelled) = "10"

    GROUP BY Month(Cancelled), c.State

    </CFQUERY>

    I'm not quite sure how to create the looping table, but I've tried to simplify it and build from there. The following basic table will output the correct results from the SQL however, it won't product the table examples from the previous post.  I'm not sure if I need to create a Structure, Array and then output results.

    <table>

    <cfoutput query="Customers">

    <tr>

    <td>#Customers.State#</td><td>  #Customers.Price#</td>

    </tr>

    </cfoutput>

    </table>

    This will product the correct calculations, but I need to group out the states into their own tables.

    UT  127.87       (New) Sum of 2 listings

    UT  95.88         (Retained) Sum of 1 listings

    UT  95.88         (Cancelled) Sum of 1 listings

    WA  13.98        (Cancelled) Sum of 2 listings