获得付款总额减去收到的金额

时间:2014-11-12 09:50:43

标签: sql-server tsql group-by common-table-expression

我有两张桌子。

1.Invoice

invoice_Id  invoice_no  client_id   date        total_price    
-----------------------------------------------------------------------------
2           INV00001    9           2014-10-15  200.00   
7           INV00002    9           2014-10-16  560.00   
8           INV00003    9           2014-10-21  100.00   
11          INV00004    9           2014-10-27  101.00   

2.Invoice_payment

InvPayment_id   client_id   Invoice_Id  receipt_no  payment_date    amount_received discount
--------------------------------------------------------------------------------------------
6               9           8           REC00002    2014-10-31      5.00            0.00

现在,我希望通过合计发票金额并减去收到的任何金额来获取客户的总金额。

预期结果:

client_id   Total_price    Due_Amount
-----------------------------------------------------------------------------
9           961.00         956.00

注意:

  1. 如果到目前为止没有付款,则会有零行。
  2. 如果支付了多笔款项,可能会有多行。
  3. 以下是我的尝试:

    ;WITH cte (clientid, invoiceid,  paid, disc)
    As
    (
        Select client_id clientId, invoice_Id invoiceId,  sum(amount_received) paid, sum(discount) disc
        From tbl_Invoice_Payment
        Group by invoice_id, client_id
    )
    Select I.client_id, invoice_Id, invoice_no, I.due_date
    ,SUM(I.total_price), Isnull(SUM(paid), 0) Paid, (SUM(Total_price) - Isnull(SUM(paid),0) - Isnull(SUM(disc),0)) Balance
    --,I.total_price, Isnull(paid, 0) Paid, (Total_price - Isnull(paid,0) - Isnull(disc,0)) Balance
    From tbl_invoice I Left join cte On I.client_id = cte.clientId 
            And I.invoice_id = cte.invoiceid
            left join tbl_client C on C.client_id = I.client_id
    group by I.client_id, invoice_Id, invoice_no, due_date, account_type, company_name, total_price, paid, disc
    order by company_name
    

    但它没有按预期工作。

2 个答案:

答案 0 :(得分:1)

您只需添加子查询即可返回付款结果并从总数中减去该值,而不是使用CTE:

SQL Fiddle Demo

架构设置

CREATE TABLE Invoice
    ([invoice_Id] int, [invoice_no] varchar(8), [client_id] int, 
     [date] datetime, [total_price] decimal(5,2));

INSERT INTO Invoice
    ([invoice_Id], [invoice_no], [client_id], [date], [total_price])
VALUES
    (2, 'INV00001', 9, '2014-10-15 00:00:00', 200.00),
    (7, 'INV00002', 9, '2014-10-16 00:00:00', 560.00),
    (8, 'INV00003', 9, '2014-10-21 00:00:00', 100.00),
    (11, 'INV00004', 9, '2014-10-27 00:00:00', 101.00);

CREATE TABLE Invoice_Payment
    ([InvPayment_id] int, [client_id] int, [Invoice_Id] int, [receipt_no] varchar(8), 
     [payment_date] datetime, [amount_received] decimal(5,2), [discount] int);

INSERT INTO Invoice_Payment
    ([InvPayment_id], [client_id], [Invoice_Id], [receipt_no], [payment_date], 
     [amount_received], [discount])
VALUES
    (6, 9, 8, 'REC00002', '2014-10-31 00:00:00', 5.00, 0.00);

查询以生成输出:

SELECT  i.client_id , SUM(i.total_price) AS Total_price,
        SUM(i.total_price) - ( SELECT   SUM(ip.amount_received)
                               FROM     dbo.Invoice_Payment ip
                               WHERE    i.client_id = ip.client_id
                             ) AS DueAmount
FROM    dbo.Invoice i
WHERE   client_id = 9
GROUP BY client_id

<强> Results

| CLIENT_ID | TOTAL_PRICE | DUEAMOUNT |
|-----------|-------------|-----------|
|         9 |         961 |       956 |

答案 1 :(得分:0)

请选择您想要的值。问题与分组有关。

group by条款I.client_idinvoice_Idinvoice_nodue_dateaccount_typecompany_name,{{1 }},total_pricepaid指定了这么多字段。 discinvoice_Id会阻止您需要的分组。从invoice_noinvoice_Id移除invoice_noselect,然后重试。