簡體   English   中英

從表信用借記列中選擇運行余額

[英]Select running balance from table credit debit columns

我有一個SQL Server 2008表,我需要從中選擇一個運行余額

TransDate    Credit    Debit    Datasource
------------------------------------------
2014-01-01   5000      NULL     3
2014-01-07   NULL      2000     3
2014-01-11   5000      NULL     3
2014-02-03   6000      NULL     4
2014-02-06   NULL      4000     4
2014-02-11   3000      NULL     4
2014-02-21   NULL      1000     3
2014-02-28   2000      NULL     3
2014-03-01   5000      NULL     3

我嘗試了相關的查詢

Select 
    t.TransDate, 
    t.Credit, 
    t.Debit, 
    (Select sum(coalesce(x.credit, 0) - coalesce(x.debit, 0))  
    From Transactions x 
    WHERE x.DataSource IN (3,4)  AND (x.TransDate >= '2014/02/01' AND x.TransDate <= '2014/02/28' ) 
    AND x.TransDate = t.TransDate) Balance
From 
    Transactions t

但是我得到了Balance所有空值。

預期的產出是

TransDate    Credit    Debit     Balance
------------------------------------------
2014-01-11   NULL      NULL      8000      <- opening balance     
2014-02-03   6000      NULL      14000
2014-02-06   NULL      4000      10000
2014-02-11   3000      NULL      13000
2014-02-21   NULL      1000      12000
2014-02-28   2000      NULL      15000     <- closing balance

嘗試這個:

Select 
    x.TransDate, 
    x.Credit, 
    x.Debit, 
    SUM(coalesce(y.credit, 0) - coalesce(y.debit, 0))  AS Balance
FROM Transactions x 
INNER JOIN Transasctions y
    ON y.TransDate <= x.TransDate
    AND Y.DataSource IN (3,4)  
WHERE x.DataSource IN (3,4)  
GROUP BY
    x.TransDate, 
    x.Credit, 
    x.Debit

請注意,對於大型數據集,這可能會變得非常快......可能想要使用光標或嘗試新的“窗口”功能。

更多信息: https//brettwgreen.wordpress.com/2012/10/17/sql-cursors-are-slow-except-when-they-arent/

你需要自己加入表。

CREATE TABLE Test
(
  TransDate DATE,
  Credit INT,
  Debit INT,
);
INSERT INTO Test VALUES
('2014-01-01',   5000,      NULL),  
('2014-01-07',   NULL,      2000),   
('2014-01-11',   5000,      NULL),   
('2014-02-03',   6000,      NULL),    
('2014-02-06',   NULL,      4000),    
('2014-02-11',   3000,      NULL),   
('2014-02-21',   NULL,      1000),     
('2014-02-28',   2000,      NULL),     
('2014-03-01',   5000,      NULL) 

WITH CTE AS
(
SELECT t2.TransDate, 
       t2.Credit, 
       t2.Debit, 
       SUM(COALESCE(t1.credit, 0) - COALESCE(t1.debit, 0)) AS Balance
FROM Test t1 
INNER JOIN Test t2
    ON t1.TransDate <= t2.TransDate
WHERE t1.DataSource IN (3,4)  
GROUP BY t2.TransDate, t2.Credit, t2.Debit
)
SELECT * 
FROM CTE
WHERE (TransDate >= '2014/01/11' AND TransDate <= '2014/02/28' ) 

OUTPUT

TransDate   Credit  Debit   Balance
2014-01-11  5000    (null)  8000
2014-02-03  6000    (null)  14000
2014-02-06  (null)  4000    10000
2014-02-11  3000    (null)  13000
2014-02-21  (null)  1000    12000
2014-02-28  2000    (null)  14000

SQL FIDDLE

我建議這樣做:

數據集

CREATE TABLE Test1(
  Id int,
  TransDate DATE,
  Credit INT,
  Debit INT
);

INSERT INTO Test1 VALUES
(1, '2014-01-01',   5000,      NULL),  
(2, '2014-01-07',   NULL,      2000),   
(3, '2014-01-11',   5000,      NULL),   
(4, '2014-02-03',   6000,      NULL),    
(5, '2014-02-06',   NULL,      4000),    
(6, '2014-02-11',   3000,      NULL),   
(7, '2014-02-21',   NULL,      1000),     
(8, '2014-02-28',   2000,      NULL),     
(9, '2014-03-01',   5000,      NULL) 

SELECT  TransDate,
        Credit, 
        Debit, 
        SUM(isnull(Credit,0) - isnull(Debit,0)) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as Balance
FROM Test1
order by TransDate

OUTPUT

TransDate   Credit  Debit   Balance
2014-01-01  5000    NULL    5000
2014-01-07  NULL    2000    3000
2014-01-11  5000    NULL    8000
2014-02-03  6000    NULL    14000
2014-02-06  NULL    4000    10000
2014-02-11  3000    NULL    13000
2014-02-21  NULL    1000    12000
2014-02-28  2000    NULL    14000
2014-03-01  5000    NULL    19000

謝謝!

以下與我合作:

WITH tempDebitCredit AS (
Select 0 As Details_ID, null As Creation_Date, null As Reference_ID, 'Brought Forward' As Transaction_Kind, null As Amount_Debit, null As Amount_Credit, isNull(Sum(Amount_Debit - Amount_Credit), 0) 'diff'
From _YourTable_Name
where Account_ID = @Account_ID
And Creation_Date < @Query_Start_Date
Union All
SELECT a.Details_ID, a.Creation_Date, a.Reference_ID, a.Transaction_Kind, a.Amount_Debit, a.Amount_Credit, a.Amount_Debit - a.Amount_Credit 'diff'
FROM _YourTable_Name a
where Account_ID = @Account_ID
And Creation_Date >= @Query_Start_Date And Creation_Date <= @Query_End_Date
)

SELECT a.Details_ID, a.Creation_Date, a.Reference_ID, a.Transaction_Kind, 
a.Amount_Debit, a.Amount_Credit, SUM(b.diff) 'Balance'
FROM   tempDebitCredit a, tempDebitCredit b
WHERE b.Details_ID <= a.Details_ID
GROUP BY a.Details_ID, a.Creation_Date, a.Reference_ID, a.Transaction_Kind, 
a.Amount_Debit, a.Amount_Credit
Order By a.Details_ID Desc

在Microsoft SQL Server上測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM