我有一个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
答案 0 :(得分:1)
试试这个:
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/
答案 1 :(得分:1)
你需要自己加入表格。
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' )
<强>输出强>
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
答案 2 :(得分:1)
我建议这样做:
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
谢谢!
答案 3 :(得分:0)
以下与我合作:
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上测试