如何将会计分录流水合并成会计分录,环境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 ('',3,'M001','C','CNY',118802.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',2,'M002','D','CNY',-118802.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',0,'U001','D','CNY',-118802.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'U002','C','CNY',118802.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',8,'P001','C','CNY',95.04);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',6,'M002','C','CNY',118707.46);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',4,'M001','D','CNY',-118802.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'U002','C','CNY',39500);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',9,'P002','C','CNY',40);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',0,'U001','D','CNY',-39500);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',6,'M002','C','CNY',39389.07);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',4,'M001','D','CNY',-39479.07);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',3,'M001','C','CNY',39479.07);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',8,'P001','C','CNY',50);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',2,'M002','D','CNY',-39479.07);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',10,'U002','C','USD',87192.6);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',9,'U001','D','USD',-87192.6);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',4,'M002','D','USD',-46.47);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',8,'P003','C','USD',4.91);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',7,'X001','D','USD',-4.91);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'M001','C','USD',87146.13);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',2,'M001','D','USD',-87146.13);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',0,'M002','D','USD',-87146.13);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',6,'Y001','C','USD',87192.6);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',4,'M002','D','USD',-46.47);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',2,'M001','D','USD',-87519.03);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'M001','C','USD',87519.03);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',0,'M002','D','USD',-87519.03);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',10,'U002','C','USD',87565.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',9,'U001','D','USD',-87565.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',8,'P003','C','USD',4.93);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',7,'X001','D','USD',-4.93);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',6,'Y001','C','USD',87565.5);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',6,'Y001','C','USD',86684.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'M001','C','USD',86637.93);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',4,'M002','D','USD',-46.47);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',0,'M002','D','USD',-86637.93);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',10,'U002','C','USD',86684.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',8,'P003','C','USD',4.88);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',7,'X001','D','USD',-4.88);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',2,'M001','D','USD',-86637.93);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',9,'U001','D','USD',-86684.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'M001','C','USD',42823.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',4,'M002','D','USD',-202);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',10,'U002','C','USD',43025.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',9,'U001','D','USD',-43025.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',0,'M002','D','USD',-42823.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',2,'M001','D','USD',-42823.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',8,'P003','C','USD',2.44);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',6,'Y001','C','USD',43025.4);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',7,'X001','D','USD',-2.44);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'U004','C','CNY',60000);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',3,'M002','D','CNY',-199.33);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',5,'P004','C','CNY',199.33);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',0,'U003','D','CNY',-60000);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',2,'M002','C','CNY',60000);
Insert into DEMO_VCH (SET_NO,SET_ID,AP_CODE,AP_DIRECTION,CUR_CODE,TX_AMT) values ('',1,'M002','D','CNY',-60000);
commit;

效果:

一段SQL的更多相关文章

  1. 评《撸一段 SQL ? 还是撸一段代码? 》

    最近看到一篇博客<撸一段 SQL ? 还是撸一段代码?>,文章举例说明了一个连表查询使用程序code来写可读性可维护性更好,但是回帖意见不一致,我想作者在理论层面没有做出更好的论述,而我今 ...

  2. 如何通过JS调用某段SQL语句

    如何通过JS调用某段SQL语句,这样的需求在报表.数据平台开发中很常见.以报表平台FineReport开发为例,例如在点击某个按钮之后,来判断一下数据库条数,再决定下一步操作.那这在后台如何实现呢? ...

  3. 撸一段 SQL ? 还是撸一段代码?

    记得刚入公司带我的研发哥们能写一手漂亮的 SQL,搜索准确.执行快.效率高. 配合Web项目中的查询展示数据的需求,基本是分分钟完成任务. 那段时间基本是仰视的态度,每天都去讨教一点手写 SQL 的要 ...

  4. FP 某段SQL语句执行时间超过1个小时,并报错:ORA-01652: 无法通过 128 (在表空间 TEMPSTG 中) 扩展

    一.出现如下两个错误:1.某一段SQL语句执行时间超过1个小时:2.一个小时后,提示如下错误:ORA-01652: 无法通过 128 (在表空间 TEMPSTG 中) 扩展 temp 段ORA-065 ...

  5. SqlServer定时跑一段SQL语句

    1.请把这段SQL语句写成一个存储过程,然后需要在B上面开启 SQL Server Agent服务,如下图: 2.开启完之后,打开数据库管理工具,然后依下图所示,展开“SQL Server Agent ...

  6. thinkphp5, 结合pgsql使用时, 要先运行这段sql代码

    按照tp5的官方文档的说法, 必须这么做: 先执行一段sql代码 CREATE OR REPLACE FUNCTION pgsql_type(a_type varchar) RETURNS varch ...

  7. C#先执行一段sql等后台操作后再提示是否后续操作confrim

    应用场景:例如选择一个单据号打击打印后先去数据库检索是否有打打印过,如果有则提示,已打印,是否再打 如果没有则不提示,直接进行打印. 实现原理:多做一个隐藏按钮去实现打印功能,页面上的打印按钮则进行数 ...

  8. 一段sql的优化

    优化前代码 select * ,ROW_NUMBER() OVER(order by WrongCount desc) as rowId from(select Quba_IDint,Quba_Num ...

  9. 一段SQL代码的压缩:从974行到96行,十倍压缩

    涉及到数据库拆分,需要将相关表的JOIN SQL剥离. 剥离过程中,发现了这么个SQL XML.整个SQL 974 行,分为6个函数.读起来费劲无比. <?xml version="1 ...

随机推荐

  1. ASP.NET提取多层嵌套json数据的方法

    本文实例讲述了ASP.NET利用第三方类库Newtonsoft.Json提取多层嵌套json数据的方法,具体例子如下. 假设需要提取的json字符串如下: {"name":&quo ...

  2. JaveScript-解决表格使用滚动条时冻结表头栏问题

    解决方法: //设置表格表头里的th==表格内容里的td function ThEqualTd(thId, tdId) { var tdNum = document.getElementById(td ...

  3. Java的基本数据类型与转换

    1.1 Java为什么需要保留基本数据类型 http://www.importnew.com/11915.html 基本数据类型对大多数业务相关或网络应用程序没有太大的用处,这些应用一般是采用客户端/ ...

  4. springboot: mybatis集成参考

    http://blog.csdn.net/isea533/article/details/50359390

  5. Linux终端使用代理服务器

    1.YUM添加代理服务器: [root@localhost /]# vim /etc/yum.conf 添加如下项目: proxy=http://172.16.1.188:8888/2.WGET添加代 ...

  6. 2016福州大学软件工程第五、六次团队作业-Alpha阶段成绩汇总

    1.本次作业成绩统计结果: 本次Alpha阶段团队作业公布如下: 表格说明: PE:贡献百分比 YS:演示评分(满分15分) BK:博客评分(满分15分) SH:事后诸葛亮环节(满分5分) P:个人分 ...

  7. ASP.NET Forms 身份验证

    ASP.NET Forms 身份验证 在开发过程中,我们需要做的事情包括: 1. 在 web.config 中设置 Forms 身份验证相关参数.2. 创建登录页. 登录页中的操作包括: 1. 验证用 ...

  8. CsvHelper支持List<T>

      /// <summary> /// Csv帮助类 /// </summary> public class CsvHelper { /// <summary> / ...

  9. 第二章 Rest框架 Nancy

    正如你看到的,Nancy有两个主要用途. 其中第一项是作为一种通用的基于 REST 框架,可替代 ASP.NET Web API 或其他Rest工具包. 默认情况下,Nancy提供一流的路由和内容协商 ...

  10. asp.net MVC4 异步文件上传

    1.下载ajaxfileupload.js 2.在控制器内创建如下方法 //文件上传 public ActionResult uploadFile(HttpPostedFileBase file) { ...