我需要改进我的T-SQL查询,以便我可以返回列的总和

时间:2010-08-25 15:06:46

标签: .net sql-server tsql

这是我当前的查询:

    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
    FROM producer
    JOIN producer_details ON producer.federal_number = producer_details.federal_number
    JOIN statement ON producer.patron_number = statement.patron_number
    WHERE producer.patron_number = @PatronNo

我需要对其进行改进,以便我可以在“付款”表格中检索资金列的总和:

 quota_payment (money)
    total_deductions (money)
    net_cheque_or_direct_deposit (money)
    interim_payment (money)
    final_payment (money)
    statement_number (int) (FK) (statement_number is the primary key in the statement table)

根据@ Quassnoi的要求,这里是示例数据和@Quassnoi的查询输出:

producer Table:
patron_number  patron_name  federal_number
20109      A. Beaton    44373

producer_details Table:
federal_number  hst_number  average_bf_test
44373       R999999999  0.0392

statement Table:
statement_number  period_ending  description         amount_shipped  patron_number
386               2009/07/30     Quota Milk Shipped  32.91           20109
387               2009/11/30     Quota Milk Shipped  22.34           20109

payment Table:
quota_payment  total_deductions  net_cheque_or_direct_deposit  interim_payment
22872.5800     7526.6500         15345.9300                    6520.7000
18474.3400     4563.2300         13911.1100                    3220.3000
final_payment   statement_number
8825.2300       386
10690.8100      387          

这是@ Quassnoi查询返回的数据(最初第二行是钱列的NULL,但事实证明那是因为那时付款表中只有一行,但是我自那以后增加了一行):

patron_name  federal_number  hst_number  average_bf_test       
A. Beaton    44373           R999999999   0.0392                           
A. Beaton    44373           R999999999   0.0392                             

statement_number   period_ending     quota_payment   total_deductions
387                2009/11/30        22872.5800      7526.6500
387                2009/11/30        18474.3400      4563.2300

net_cheque_or_direct_deposit   interim_payment      final_payment
15345.9300                     6520.7000            8825.2300
13911.1100                     3220.3000            10690.8100

这是所需的记录集:

patron_name  federal_number  hst_number  average_bf_test       
A. Beaton    44373           R999999999  0.0392   

statement_number   period_ending     quota_payment   total_deductions
387                2009/11/30        41346.92        12089.88


net_cheque_or_direct_deposit   interim_payment      final_payment
29257.04                        9741.00             19516.04

我忘了提到钱栏的总和值应该是“年初至今”的值(意味着上一年的值不会包含在SUM结果中),这使得它更复杂一些。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

你可以试试这个:

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 (Quassnois Query here) a 
group by 
patron_name, 
federal_number, 
hst_number, 
average_bf_test, 
statement_number, 
period_ending

根据您发布的数据,这应该可以满足您的需求。我不知道这在逻辑上是否适合你的情况,但它应该有效。

答案 2 :(得分:0)

如果我理解你想做什么,这应该有效,不测试语法......

根据您的评论,也许您正在寻找:

Select a.*,sum(quota_payment) as sum_quota_payment, 
    sum(total_deductions) as sum_total_deductions,
    sum(net_cheque_or_direct_deposit) as sum_net_cheque, 
    sum(interim_payment) as sum_interim_payment, 
    sum(final_payment) as sum_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 
            FROM producer 
            JOIN producer_details ON producer.federal_number = producer_details.federal_number 
            JOIN statement ON producer.patron_number = statement.patron_number 
            WHERE producer.patron_number = @PatronNo) 
    a left join payment ON a.statement_number = payment.statement_number