通过每周交易汇总重建余额

时间:2019-02-20 15:09:15

标签: sql-server

我正在寻找有关如何构造此方法的建议或指示。我花了去年的自学SQL。我正在工作,并且只能访问报表生成器中的查询界面。对我来说,这意味着没有程序,没有创建表,没有IDE :(。这就是局限性!

我正在尝试重建帐户余额。我没有中间余额。我有当前余额和一个充满交易历史记录的表

我当前的方法是通过在名为CTE的CTE中发布星期(我已经完成)来汇总交易

[SUMTRANSREF]
+--------------+------------+-----------+
| TNCY-SYS-REF | POSTING-WK | SUM-TRANS |
+--------------+------------+-----------+
|            1 |         47 | 37.95     |
|            1 |         46 | 37.95     |
|            1 |         45 | 37.95     |
|            2 |         47 | 50.00     |
|            2 |         46 | 25.00     |
|            2 |         45 | 25.00     |
+--------------+------------+-----------+

然后我在另一个名为CTE的CTE中获得当前余额

[CBAL]
+--------------+-------------+-----------+
| TNCY-SYS-REF | CUR-BALANCE | CURR-WEEK |
+--------------+-------------+-----------+
|            1 | 27.52       |        47 |
|            1 | 52.00       |        47 |
+--------------+-------------+-----------+

现在我假设我可以创建中间的CTE以求和,然后将它们全部拼接起来,但是有没有更聪明(更自动化)的方式?

理想情况下,我的结果应该是

+--------------+-------------+----------+----------+
| TNCY-SYS-REF | CUR-BALANCE | BAL-WK46 | BAL-Wk45 |
+--------------+-------------+----------+----------+
|            1 | 27.52       | -10.43   | -48.38   |
|            2 | 52.00       | 2.00     | -48.00   |
+--------------+-------------+----------+----------+

我不确定,因为每一列都需要介入交易的总和

So BAL-WK46 is (CURR-BALANCE) - SUM(Transactions from 47)
So BAL-WK46 is (CURR-BALANCE) - SUM(Transactions 46+47)
So BAL-WK45 is (CURR-BALANCE) - SUM(Transactions 45+46+47)

以此类推。

通常我知道从哪里开始,但是我对此感到困惑。

您能提供的任何帮助将不胜感激。谢谢

1 个答案:

答案 0 :(得分:1)

以下是一些T-SQL,可满足您所需的结果。应该足够容易玩才能得到想要的东西。

它使用了递归CTE和PIVOT

IF OBJECT_ID('Tempdb..#SUMTRANSREF') IS NOT NULL
DROP TABLE #SUMTRANSREF

IF OBJECT_ID('Tempdb..#CBAL') IS NOT NULL
DROP TABLE #CBAL


IF OBJECT_ID('Tempdb..#TEMP') IS NOT NULL
DROP TABLE #TEMP

CREATE TABLE #SUMTRANSREF

(
[TNCY-SYS-REF] int,
[POSTING-WK]  int,
[SUM-TRANS] float
)

CREATE TABLE #CBAL
(
 [TNCY-SYS-REF] int ,
  [CUR-BALANCE] float , [CURR-WEEK] int

)

INSERT INTO #SUMTRANSREF
VALUES (1 ,47 , 37.95),
(1 ,46 , 37.95),
(1 ,45 , 37.95),
(2 ,47 , 50.00),
(2 ,46 , 25.00),
(2 ,45 , 25.00 ) 


INSERT INTO #CBAL
VALUES (1,27.52,47),(2,52.00,47);


WITH CBAL AS
(SELECT * FROM #CBAL),

SUMTRANSREF AS(SELECT * FROM #SUMTRANSREF),
RecursiveTotals([TNCY-SYS-REF],[CURR-WEEK],[CUR-BALANCE],RunningBalance) 
AS
(
select  C.[TNCY-SYS-REF], C.[CURR-WEEK],C.[CUR-BALANCE],C.[CUR-BALANCE] + S.RunningTotal RunningBalance from CBAL C
JOIN (select *,-SUM([SUM-TRANS]) OVER (PARTITION BY [TNCY-SYS-REF] ORDER BY [POSTING-WK] DESC) RunningTotal 
from SUMTRANSREF) S
ON C.[CURR-WEEK]=S.[POSTING-WK] AND C.[TNCY-SYS-REF]=S.[TNCY-SYS-REF]
UNION ALL
select  RT.[TNCY-SYS-REF], RT.[CURR-WEEK] -1 [CURR_WEEK],RT.[CUR-BALANCE],RT.[CUR-BALANCE] + S.RunningTotal RunningBalance  FROM  RecursiveTotals RT
JOIN (select *,-SUM([SUM-TRANS]) OVER (PARTITION BY [TNCY-SYS-REF] ORDER BY [POSTING-WK] DESC) RunningTotal 
from #SUMTRANSREF) S ON   RT.[TNCY-SYS-REF] = S.[TNCY-SYS-REF] AND RT.[CURR-WEEK]-1 = S.[POSTING-WK]
)

select [TNCY-SYS-REF],[CUR-BALANCE],[46] as 'BAL-WK46',[45] as 'BAL-WK45',[44] as 'BAL-WK44'
FROM (
select [TNCY-SYS-REF],[CUR-BALANCE],RunningBalance,BalanceWeek from (SELECT *,R.[CURR-WEEK]-1 'BalanceWeek'  FROm RecursiveTotals R
) RT) AS SOURCETABLE
PIVOT
(
    AVG(RunningBalance)
    FOR  BalanceWeek in ([46],[45],[44])
) as PVT