SQL Server 2008 R2——用CTE進行遞歸計算求解累計值


=================================版權聲明=================================

版權聲明:原創文章 謝絕轉載 

請通過右側公告中的“聯系郵箱(wlsandwho@foxmail.com)”聯系我

勿用於學術性引用。

勿用於商業出版、商業印刷、商業引用以及其他商業用途。                   

 

本文不定期修正完善。

本文鏈接:http://www.cnblogs.com/wlsandwho/p/4968075.html

恥辱牆:http://www.cnblogs.com/wlsandwho/p/4206472.html

=======================================================================

話不多說 先上問題

鄙視垃圾爬蟲網站 祝你們早生極樂

=======================================================================

乍看群友的敘述,是要想很多事情的,大概兩分鍾(虛詞)后,就可以分析出

余額'=余額+借方-貸方

然而關鍵的一點是,1+0=1,1-0=1。此為小學知識。意思是加減零對運算結果不影響。

所以問題中需要進行判斷的地方只有“方向”列。而“方向”列的計算依據是余額,所以只要先專心致志的求解出“余額”列就行了。

(此處省略了群友自己給出的分析,沒有為什么。經常在群里回答問題的人都知道這其中的厲害。)

=======================================================================

承蒙園友ahdung在上一篇博文(傳送門)里不吝賜教,此處王林森嘗試着使用CTE的遞歸進行問題求解。

=======================================================================

下面貼上王林森的代碼(附帶測試數據)

 1 -----------------------------------------------------------
 2 --網絡代碼有風險
 3 --復制粘貼須謹慎
 4 --wls 20151116
 5 USE tempdb
 6 GO
 7 
 8 IF OBJECT_ID (N't_DCRbyWLS', N'U') IS NOT NULL
 9 DROP TABLE t_DCRbyWLS;
10 GO
11 
12 CREATE TABLE t_DCRbyWLS(Debtor REAL,Creditor REAL,Direction NVARCHAR(1),Remainder REAL)
13 GO
14 
15 INSERT INTO t_DCRbyWLS(Direction,Remainder) VALUES ('',84.9000)
16 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (3000.000,0.0000)
17 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,800.0000)
18 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,2284.9000)
19 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (1144.0000,0.0000)
20 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,1144.0000)
21 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (5000.0000,0.0000)
22 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,5000.0000)
23 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (436.0000,0.0000)
24 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,436.0000)
25 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,4000.0000)
26 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (5000.0000,0.0000)
27 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,960.0000)
28 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,800.0000)
29 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (800.0000,0.0000)
30 INSERT INTO t_DCRbyWLS(Debtor,Creditor) VALUES (0.0000,40.0000)
31 GO
32 
33 SELECT * FROM t_DCRbyWLS
34 GO
35 
36 WITH TempDCR
37 AS
38 (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS 'ID' ,Debtor,Creditor,Direction,Remainder FROM t_DCRbyWLS)
39 , 
40 TempReCursion
41 AS
42 (
43 SELECT TOP 1 ID, Debtor,Creditor,Remainder,Direction FROM TempDCR
44 UNION ALL
45 SELECT a.ID,a.Debtor,a.Creditor,b.Remainder+a.Debtor-a.Creditor,Direction=CASE WHEN b.Remainder+a.Debtor-a.Creditor>=0 THEN N'' ELSE N'' END 
46     FROM TempDCR a JOIN TempReCursion b ON a.ID=b.ID+1
47 )
48 SELECT Debtor,Creditor,Direction,Remainder FROM TempReCursion

鄙視垃圾爬蟲網站 祝你們早生極樂

運行結果如下:

鄙視垃圾爬蟲網站 祝你們早生極樂

作為一只C++,我做SQL的宗旨是“不求高效,但求能跑”。

下面附上執行計划:

鄙視垃圾爬蟲網站 祝你們早生極樂

=======================================================================

(友情支持請掃描這個)

微信掃描上方二維碼捐贈

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM