我的T-SQL查询需要来自附加表的额外SUM

时间:2010-08-27 03:37:27

标签: .net sql-server tsql

以下是我当前的查询。它按照我想要的方式工作。但是,它需要一些改进,以便我可以1)检索我的'花费'表中的money列的SUM和2)添加WHERE子句(或HAVING?),以便所有的SUM都是年初至今的值,例如2010年1月1日之前的任何金额都不会被SUM函数计算。

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

以下是我的“费用”表:

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)

以下是“费用”表中的数据:

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 个答案:

答案 0 :(得分:1)

如果我理解正确,以下内容应该有效:

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

但是,我认为你可以采取更多措施使这个查询运行得更好 - 例如,statement上的多个子选择似乎很容易成为优化/重构的目标。