如何将会计分录流水合并成会计分录,环境oracle 11g,代码如下:
表:
CREATE TABLE "DEMO_VCH" ("SET_NO" VARCHAR2(45 BYTE), "SET_ID" NUMBER, "AP_CODE" VARCHAR2(24 BYTE), "AP_DIRECTION" VARCHAR2(1 BYTE), "CUR_CODE" VARCHAR2(3 BYTE), "TX_AMT" NUMBER(24,4) ) ; COMMENT ON COLUMN DEMO_VCH.SET_NO is '会计分录套号'; COMMENT ON COLUMN DEMO_VCH.SET_ID is '会计分录序号'; COMMENT ON COLUMN DEMO_VCH.AP_CODE is '科目码'; COMMENT ON COLUMN DEMO_VCH.AP_DIRECTION is '接待方向(D:借;C:贷)'; COMMENT ON COLUMN DEMO_VCH.CUR_CODE is '币别'; COMMENT ON COLUMN DEMO_VCH.TX_AMT is '金额';
查询:
WITH VCH AS ( SELECT T.*, SUM( CASE WHEN AP_DIRECTION='D' AND AP_D2! ='D' THEN 1 ELSE 0 END)OVER(PARTITION BY SET_NO ORDER BY SET_ID) RN --分段求和 FROM ( SELECT T.*, LAG(AP_DIRECTION,1,'X')OVER(PARTITION BY SET_NO ORDER BY SET_ID) AP_D2 --上一行的值 FROM DEMO_VCH T ) T ) , D AS ( SELECT * FROM VCH WHERE AP_DIRECTION='D' ) , C AS ( SELECT * FROM VCH WHERE AP_DIRECTION='C' ) SELECT D.SET_NO, D.RN AS SET_ID, D.AP_CODE AS D_APCODE, D.AP_DIRECTION AS D_DIRECTION, D.CUR_CODE AS D_CUR, D.TX_AMT AS D_AMT, C.AP_CODE AS C_APCODE, C.AP_DIRECTION AS C_DIRECTION, C.CUR_CODE AS C_CUR, C.TX_AMT AS C_AMT FROM D FULL JOIN C ON D.SET_NO=C.SET_NO AND D.RN =C.RN;
数据:
SET DEFINE OFF; Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100001',3,'M001','C','CNY',118802.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100001',2,'M002','D','CNY',-118802.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100001',0,'U001','D','CNY',-118802.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100001',1,'U002','C','CNY',118802.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100001',8,'P001','C','CNY',95.04); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100001',6,'M002','C','CNY',118707.46); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100001',4,'M001','D','CNY',-118802.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',1,'U002','C','CNY',39500); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',9,'P002','C','CNY',40); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',0,'U001','D','CNY',-39500); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',6,'M002','C','CNY',39389.07); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',4,'M001','D','CNY',-39479.07); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',3,'M001','C','CNY',39479.07); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',8,'P001','C','CNY',50); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100002',2,'M002','D','CNY',-39479.07); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',10,'U002','C','USD',87192.6); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',9,'U001','D','USD',-87192.6); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',4,'M002','D','USD',-46.47); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',8,'P003','C','USD',4.91); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',7,'X001','D','USD',-4.91); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',1,'M001','C','USD',87146.13); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',2,'M001','D','USD',-87146.13); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',0,'M002','D','USD',-87146.13); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100003',6,'Y001','C','USD',87192.6); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',4,'M002','D','USD',-46.47); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',2,'M001','D','USD',-87519.03); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',1,'M001','C','USD',87519.03); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',0,'M002','D','USD',-87519.03); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',10,'U002','C','USD',87565.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',9,'U001','D','USD',-87565.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',8,'P003','C','USD',4.93); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',7,'X001','D','USD',-4.93); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100004',6,'Y001','C','USD',87565.5); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',6,'Y001','C','USD',86684.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',1,'M001','C','USD',86637.93); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',4,'M002','D','USD',-46.47); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',0,'M002','D','USD',-86637.93); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',10,'U002','C','USD',86684.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',8,'P003','C','USD',4.88); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',7,'X001','D','USD',-4.88); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',2,'M001','D','USD',-86637.93); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100005',9,'U001','D','USD',-86684.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',1,'M001','C','USD',42823.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',4,'M002','D','USD',-202); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',10,'U002','C','USD',43025.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',9,'U001','D','USD',-43025.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',0,'M002','D','USD',-42823.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',2,'M001','D','USD',-42823.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',8,'P003','C','USD',2.44); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',6,'Y001','C','USD',43025.4); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100006',7,'X001','D','USD',-2.44); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100007',1,'U004','C','CNY',60000); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100007',3,'M002','D','CNY',-199.33); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100007',5,'P004','C','CNY',199.33); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100007',0,'U003','D','CNY',-60000); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100008',2,'M002','C','CNY',60000); Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('100008',1,'M002','D','CNY',-60000); commit;
效果: