我想知道是否有人可以帮助我改进我的T-SQL查询,以便我可以从另一个表中检索钱列的SUM ... :)

时间:2010-09-03 19:15:54

标签: .net sql tsql

首先,非常感谢你提前帮助我解决这个问题。我当前的查询(下面)按照我希望它保存标题中提到的要求的方式工作:我需要检索我的'费用'表中的money列的SUM,并且每个值必须对应于period_ending值,即大于@FiscalYearEnd,与此行中的其他SUM值相同:

JOIN statement so ON so.patron_number = producer.patron_number AND so.period_ending > @FiscalYearEnd

以下是整个查询:

SELECT
patron_name, 
federal_number, 
hst_number, 
average_bf_test, 
statement_number, 
period_ending, 
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 (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 > @FiscalYearEnd
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  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 :(得分:2)

没有任何方法可以测试。我认为以下查询将使您沿着正确的方向前进,同时还纠正可能的问题(例如性能,重复工作,不必要的分组或连接)。看看以下内容,让我知道它是如何工作的。

SELECT  patron_name
    ,federal_number
    ,hst_number
    ,average_bf_test
    ,statement_number
    ,period_ending
    ,payments.quota_payments_sum AS quota_payment
    ,payments.total_deductions_sum AS total_deductions
    ,payments.net_cheque_or_direct_deposit_sum AS net_cheque_or_direct_deposit
    ,payments.interim_payment_sum AS interim_payment
    ,payments.final_payment_sum AS final_payment 
    ,expenses.milk_promotion_and_adv_sum AS milk_promotion_and_adv_expense
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 > @FiscalYearEnd
    CROSS APPLY
    (
        SELECT  SUM(quota_payment) AS quota_payment_sum,
                SUM(total_deductions) AS total_deductions_sum,
                SUM(net_cheque_or_direct_deposit) AS net_cheque_or_direct_deposit_sum,
                SUM(interim_payment) AS interim_payment_sum,
                SUM(final_payment) AS final_payment_sum
        FROM    payment p
        WHERE   p.statement_number = so.statement_number
    ) payments
    CROSS APPLY
    (
        SELECT  SUM(milk_promotion_and_adv) AS milk_promotion_and_adv_sum,
                -- etc
        FROM    expenses e
        WHERE   e.statement_number = so.statement_number
    ) expenses