Sql 从表贷方借方列中选择运行余额

Sql 从表贷方借方列中选择运行余额,sql,sql-server-2008,select,cumulative-sum,Sql,Sql Server 2008,Select,Cumulative Sum,我有一个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

我有一个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
试试这个:

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
请注意,对于大型数据集,这可能会很快变得糟糕。。。可能需要使用光标或尝试新的“窗口”功能


更多信息:

您需要自联接表

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' ) 

我建议这样做:

数据集 解决方案 输出
谢谢大家!

以下几点对我起了作用:

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上测试

您从哪里获得余额8000?你能提供SQL Fiddle吗?8000是上个月的余额,发布的预期结果是“2014/02/01”和“2014/02/28”@Smith检查我的答案。添加了SQL FIDLE我收到一个错误,多部分标识符t.TransDate无法绑定。可以找出为什么固定别名。。请尝试我修改后的答案查询正确,但它返回的所有结果都没有日期范围,我注意到您在刚才midified的答案中省略了日期?如果需要,您可以将它们添加回。。。这取决于你想做什么。与转换y的连接没有日期限制,因此它仍应为您提供正确答案。可能还需要在数据源上筛选y。。。取决于表中的内容。谢谢你的回答,但期初余额应该是8000,而不是11000,其他余额与我在question@Smith向我提供什么是数据源。我在事务表TransDate Credit Debit中只看到了3列,请再次看到这个问题:祝您好运,编码愉快。我需要改进我的sql查询编写,我可以使用哪本书或哪种资源。你用了哪些或者你能推荐哪些??
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
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
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