views:

79

answers:

1

The following is my current query. It works the way i want it to. However, it needs some improvements so that i can 1) retrieve the SUM's of the money columns in my 'expenses' table and 2) add a WHERE clause (or HAVING?) so that all of the SUM's are year-to-date values, e.g. any amounts prior to Jan. 1, 2010 will not be totaled by the SUM functions.

SELECT 
patron_name, 
federal_number, 
hst_number, 
average_bf_test, 
statement_number, 
period_ending, 
SUM(quota_payment) AS ytd_quota_payment, 
SUM(total_deductions) AS ytd_total_deductions, 
SUM(net_cheque_or_direct_deposit) AS ytd_net_cheque_or_direct_deposit, 
SUM(interim_payment) AS ytd_interim_payment, 
SUM(final_payment) AS ytd_final_payment 
FROM (SELECT  patron_name, 
producer.federal_number, 
hst_number, 
average_bf_test,
(
SELECT  MAX(s.statement_number)
FROM    statement s
) AS statement_number,
(
SELECT MAX(s.period_ending) 
FROM   statement s
) AS period_ending,
sums.*
FROM    producer
JOIN    producer_details
ON      producer_details.federal_number = producer.federal_number
JOIN    statement so
ON      so.patron_number = producer.patron_number
CROSS APPLY
(
SELECT  SUM(quota_payment) AS quota_payment,
        SUM(total_deductions) AS total_deductions,
        SUM(net_cheque_or_direct_deposit) AS net_cheque_or_direct_deposit,
        SUM(interim_payment) AS interim_payment,
        SUM(final_payment) AS final_payment
FROM    payment p
WHERE   p.statement_number = so.statement_number
) sums
WHERE   producer.patron_number = @PatronNo) a 

GROUP BY 
patron_name, 
federal_number, 
hst_number, 
average_bf_test, 
statement_number, 
period_ending

The following is my 'expenses' table:

cgm_validation (money)
milk_promotion_and_adv (money)
trucking (money)
loan_capital (money)
dfns_producer_dues (money)
sundry_deduction (money)
credited_to_account (money)
hst (money)
statement_number (int)

Here is the data in my 'expenses' table:

cgm_validation  milk_promotion_and_adv  trucking  loan_capital  dfns_producer_dues 
0.0             387.41                  950.25    487.34        134.92             
0.0             525.67                  812.47    501.12        162.93                                             
sundry_deduction  credited_to_account   hst       statement_number
4987.56           0.0                   182.34    386
5620.34           0.0                   234.28    387
+1  A: 

If I understood correctly, the following should work:

SELECT 
a.patron_name, 
a.federal_number, 
a.hst_number, 
a.average_bf_test, 
a.statement_number, 
a.period_ending, 
SUM(a.quota_payment) AS ytd_quota_payment, 
SUM(a.total_deductions) AS ytd_total_deductions, 
SUM(a.net_cheque_or_direct_deposit) AS ytd_net_cheque_or_direct_deposit, 
SUM(a.interim_payment) AS ytd_interim_payment, 
SUM(a.final_payment) AS ytd_final_payment 
FROM (
        SELECT  patron_name, 
        producer.federal_number, 
        hst_number, 
        average_bf_test,
        (
            SELECT  MAX(s.statement_number)
            FROM    statement s
        ) AS statement_number,
        (
            SELECT MAX(s.period_ending) 
            FROM   statement s
        ) AS period_ending,
        sums.*
        FROM    producer
        JOIN    producer_details ON producer_details.federal_number = producer.federal_number
        JOIN    statement so ON (so.patron_number = producer.patron_number and so.period_ending > '01/01/2010')
        CROSS APPLY
        (
            SELECT  SUM(quota_payment) AS quota_payment,
                    SUM(total_deductions) AS total_deductions,
                    SUM(net_cheque_or_direct_deposit) AS net_cheque_or_direct_deposit,
                    SUM(interim_payment) AS interim_payment,
                    SUM(final_payment) AS final_payment
            FROM    payment p
            WHERE   p.statement_number = so.statement_number
        ) sums
        WHERE   producer.patron_number = @PatronNo
) a join expenses e on (a.statement_number = e.statement_number)
GROUP BY 
a.patron_name, 
a.federal_number, 
a.hst_number, 
a.average_bf_test, 
a.statement_number, 
a.period_ending

However, I think there's more you can do to make this query run a little better - for example, the multiple sub-selects on statement seem likely to be an easy target for optimization / refactoring.

TML
That doesn't work. I get two syntax errors: one near 'CROSS' and one near 'sums' :\
brookmarker
Syntax error on my part - I left the closing ) off the expanded JOIN clause. I've fixed it above.
TML
and so.period_ending > '01/01/2010') <-- was missing
TML
I'm getting the following error now: Invalid column name 'statement_number'.Ambiguous column name 'statement_number'.
brookmarker
Fixed - failed to reference the table in the group by, and misspelled the column name in the join to expenses. :)
TML
After trying to execute the query i get the following error: Cannot find either column "a" or the user-defined function or aggregate "a.SUM", or the name is ambiguous :\
brookmarker
I accidentally put the 'a.' in front of the sum instead of inside, all those 'a.SUM(xxxx)' should read 'SUM(a.xxxx)'
TML
Okay, it's working now, except it's not grabbing the year-to-date SUM's from the expenses table.
brookmarker
I didn't query any values from the expenses table because it wasn't clear to me from the question which things you wanted - but you should be able to add them by using 'SUM(e.whatevercol)'
TML
When i add the 'SUM(e.whatevercol)'s beginning immediately after "SUM(a.final_payment) AS ytd_final_paymen", the query does not total both columns in my 'expenses' table (i added the sample data in my 'expenses' table above); what it does is total each amount in the second row as if they were in two separate rows, e.g.: 525.67 in the 'milk_promotion_and_adv' column is returned as 1051.34 (i.e. 525.67 * 2 rather than 525.67 + 387.41)
brookmarker
They can't be sum()'d because you are grouping by statement_number. I can't see any obvious reason why they're duplicated (resulting it in being *2).
TML
Okay, i added a 'patron_number' column to my 'expenses' table and i changed this line: "join expenses e on (a.statement_number = e.statement_number)" to this: "join expenses e on (@PatronNo = e.patron_number)". Now it is working closer to what i want. It is summing the amounts in the 'expenses' table, however, it is also multiplying the total by 2 for some reason, e.g. the returned result for 'milk_promotion_and_adv' is 1826.16 rather than 913.08.
brookmarker
This would imply you have multiple rows in 'expenses' that match the join condition.
TML
so, is there anything u could recommend that i could change?
brookmarker
You're going to have to understand the data a little bit better than I do in order to fix this problem. Try finding a statement_number that is doubling, and doing a "select * from expenses where statement_number = XXXX" and see if you can find where the duplication exists.
TML
okay, it's working now. thank you very much for your help (whoever u are) :)
brookmarker