获得借记卡和余额问题

时间:2016-01-06 22:06:36

标签: sql sql-server sql-server-2008

我使用以下查询从表语句

中获取以下信息
SELECT [Statement_ID] as SID, t.[InvoiceID], t.S_Type as Type,
       t.Description, t.Date, t.Debit, t.Credit, b.Balance
FROM Statement as t CROSS apply
     (SELECT Balance = SUM(Debit) - SUM(Credit)
      FROM Statement as x
      WHERE (x.date < t.date or
             x.date = t.date and x.[Statement_ID] <= t.[Statement_ID]
            ) AND
            x.CustID = t.CustID
     ) b
WHERE t.CustID ='48' and date between '2015-01-01' and '2016-01-01'
ORDER BY t.date

out put

SID InvoiceID   Type            Date    Debit   Credit  Balance
176     51  Service Invoice 2015-08-29  500.00  0.00    500.00
462     51  Receipt Voucher 2015-09-07  0.00    500.00  0.00
107     76  Service Invoice 2015-09-28  1000.00 0.00    1000.00
165     208 Service Invoice 2015-09-28  500.00  0.00    1500.00
217     119 Service Invoice 2015-10-31  500.00  0.00    2000.00
459     76  Receipt Voucher 2015-11-21  0.00    500.00  1500.00
460     208 Receipt Voucher 2015-11-21  0.00    500.00  1000.00
461     119 Receipt Voucher 2015-11-21  0.00    500.00  500.00
163     165 Service Invoice 2015-12-01  500.00  0.00    1000.00
458     165 Receipt Voucher 2015-12-22  0.00    500.00  500.00
44      224 Service Invoice 2015-12-31  500.00  0.00    1000.00

首先,我如何获得发票下的每张收据凭证

第二,当我有相同的发票日期的收据凭证时我怎么能确定它使用Statement_ID在发票下,因为收据凭证总是在发票后输入

预期的输出

按日期订购发票并获取收据凭证

S_ID    InvoiceID   Type    Date        Debit   Credit  Balance
176     51  Service Invoice 2015-08-29  500.00  0.00    500.00
462     51  Receipt Voucher 2015-09-07  0.00    500.00  0.00
107     76  Service Invoice 2015-09-28  1000.00 0.00    1000.00
459     76  Receipt Voucher 2015-11-21  0.00    500.00  500.00
165     208 Service Invoice 2015-09-28  500.00  0.00    1000.00
460     208 Receipt Voucher 2015-11-21  0.00    500.00  500.00
217     119 Service Invoice 2015-10-31  500.00  0.00    1000.00
461     119 Receipt Voucher 2015-11-21  0.00    500.00  500.00
163     165 Service Invoice 2015-12-01  500.00  0.00    1000.00
458     165 Receipt Voucher 2015-12-22  0.00    500.00  500.00
44      224 Service Invoice 2015-12-31  500.00  0.00    1000.00

1 个答案:

答案 0 :(得分:1)

这就是......

With    CTE1 AS
(
        SELECT [Statement_ID] as SID, t.[InvoiceID], t.S_Type as Type,
               t.Description, t.Date, t.Debit, t.Credit, b.Balance
        FROM Statement as t CROSS apply
             (SELECT Balance = SUM(Debit) - SUM(Credit)
              FROM Statement as x
              WHERE (x.date < t.date or
                     x.date = t.date and x.[Statement_ID] <= t.[Statement_ID]
                    ) AND
                    x.CustID = t.CustID
             ) b
        WHERE t.CustID ='48' and date between '2015-01-01' and '2016-01-01'
),      CTE2 AS
(
        SELECT  Row_Number() Over (Order By [Date]) As priorityID,
                [InvoiceID]
        FROM    CTE1
        WHERE   [Type] = 'Service Invoice'
)
Select  c1.*
From    CTE1 c1
JOIN    CTE2 c2
        ON c1.[InvoiceID] = c2.[InvoiceID]
ORDER BY c2.priorityID, c1.[Date]