db2中的with用法,可以實現迴圈累加

2021-07-04 01:18:55 字數 1051 閱讀 5679

臨時表session.temp是如下的格式 

khdxdh jylsh   dffse     jffse  zhye1    num    zhye

1              1          10      0       10            1         20

1              55        10      0       10            2         30

1              55        10      0       10            3         40

1              88        10      5       10            4         45

通過以下的with  as語句可以實現累加,先查出num=1的記錄作為sel,然後和表 session.temp做合併,db2的不可以使用join連線 只好 

select * from  session.temp a,sel b 這種合併的方式啦。

insert into session.tmp_dfzhjl(khdxdh,jylsh,zhye)

with sel(khdxdh,jylsh,zhye,num) as

( select khdxdh,jylsh,(zhye1+dffse-jffse) as zhye,num

from session.temp where num=1

union all

select a.khdxdh,a.jylsh,(b.zhye+a.dffse-a.jffse) as zhye,a.num

from session.temp a,sel b

where a.num=b.num+1 and a.khdxdh=b.khdxdh

)select khdxdh,jylsh,zhye

from sel;

DB2中db2命令引數說明

db2中db2命令引數說明 a 顯示 sqlca off c 自動落實 on d 檢索並顯示 xml 宣告 off e 顯示 sqlcode sqlstate off f 讀取輸入檔案 off i 顯示 xml 資料並帶有縮排 off l 將命令記錄到歷史記錄檔案中 off m 顯示受影響的行數 o...

DB2中實現等級查詢一

目錄表的資料結構,為了方便,採用了統一的資料型別 create table folder folderid varchar 20 本目錄的標識 parentid varchar 20 父目錄的標識 name varchar 20 目錄名 status varchar 20 目錄狀態 建乙個函式,通過...

DB2中REVERSE函式的實現方法

oracle sql select reverse 1234 from dual reverse 4321 sql select reverse 12121 from dual select revrgohpvdfvberse 12121 from dual 第 1 行出現錯誤 ora 00932 ...