我有桌子
t1(插入客户数据)
Date id idCl nameCl opening
1-8-2016 10L CL-J Jon 0
15-8-2016 20L CL-B Ben 0
25-8-2016 15.5L CL-A Alina 0
28-8-2016 10L CL-B Ben 0
30-8-2016 20L CL-J Jon 0
t2(客户收到的付款)
Date id idCl nameCl receive
1-9-2016 10L CL-J Jon 2000
1-9-2016 10L CL-J Jon 1000
5-9-2016 20L CL-B Ben 3000
10-9-2016 15.5L CL-A Alina 5000
12-9-2016 10L CL-B Ben 8000
22-9-2016 20L CL-J Jon 2000
我希望t3像这样
Date id idCl nameCl opening dr closing
1-9-2016 10L CL-J Jon 0 2000 2000
22-9-2016 20L CL-J Jon 2000 1000 3000
1-9-2016 10L CL-J Jon 3000 2000 5000
5-9-2016 20L CL-B Ben 0 3000 3000
12-9-2016 10L CL-B Ben 3000 8000 11000
10-9-2016 15.5L CL-A Alina 0 5000 5000
主要部分是如何在每笔交易中使期末余额达到期初余额。
答案 0 :(得分:0)
这是一个由两部分组成的解决方案
第一部分(在最初的mcve-makig之后)是一个最小的查询,以接近你想要的输出(除了9月22日的奇怪排序; 10日之前的第12个是因为idCl排序更高的优先级)。
第二部分花费了相当多的努力按日期排序,相应地格式化。
-- make my mcve
-- (including unused table t1, as a nicety to others wanting to answer)
drop table if exists t1 ;
create table t1 (Date date, id varchar(10), idCl varchar(10), nameCl varchar(30), opening int);
drop table if exists t2;
create table t2 (Date date, id varchar(10), idCl varchar(10), nameCl varchar(30), receive int);
insert into t1 values ('1-8-2016', '10L', 'CL-J', 'Jon', 0);
insert into t1 values ('15-8-2016', '20L', 'CL-B', 'Ben', 0);
insert into t1 values ('25-8-2016', '15.5L', 'CL-A', 'Alina', 0);
insert into t1 values ('28-8-2016', '10L', 'CL-B', 'Ben', 0);
insert into t1 values ('30-8-2016', '20L', 'CL-J', 'Jon', 0);
insert into t2 values ('1-9-2016', '10L', 'CL-J', 'Jon', 2000);
insert into t2 values ('1-9-2016', '10L', 'CL-J', 'Jon', 1000);
insert into t2 values ('5-9-2016', '20L', 'CL-B', 'Ben', 3000);
insert into t2 values ('10-9-2016', '15.5L', 'CL-A', 'Alina', 5000);
insert into t2 values ('12-9-2016', '10L', 'CL-B', 'Ben', 8000);
insert into t2 values ('22-9-2016', '20L', 'CL-J', 'Jon', 2000);
.headers off
select '-- simple query, not sorting by date';
.headers on
-- making pretty
.mode column
select
a.date,
a.id, a.idCl, a.nameCL,
sum(b.receive)-a.receive as opening,
a.receive as dr,
sum(b.receive) as closing
from t2 as a inner join t2 as b using (idCl)
where a.rowid >= b.rowid
group by a.rowid
order by a.idCl DESC
;
-- preparing the use of date for sorting
drop table if exists moneys;
create table moneys (Date date, id varchar(10), idCl varchar(10), nameCl varchar(30), amount int);
insert into moneys select
date( substr(date,-4,4)
||'-'||substr('0'||replace(substr(date, instr(date, '-'), 3),'-', ''),-2,2)
||'-'||substr('0'||replace(substr(date, 1,2),'-',''),-2,2)
),
id,
idCl,
nameCl,
receive
from t2;
.headers off
select '';
select '-- advanced query, sorting by correct date';
.headers on
select
a.date, a.id, a.idCl, a.nameCL,
sum(b.amount)-a.amount as opening,
a.amount as dr,
sum(b.amount) as closing
from moneys as a inner join moneys as b using (idCl)
where a.rowid >= b.rowid
group by a.rowid
order by a.idCl DESC , a.date
;
输出:
-- simple query, not sorting by date
Date id idCl nameCl opening dr closing
---------- ---------- ---------- ---------- ---------- ---------- ----------
1-9-2016 10L CL-J Jon 0 2000 2000
1-9-2016 10L CL-J Jon 2000 1000 3000
22-9-2016 20L CL-J Jon 3000 2000 5000
5-9-2016 20L CL-B Ben 0 3000 3000
12-9-2016 10L CL-B Ben 3000 8000 11000
10-9-2016 15.5L CL-A Alina 0 5000 5000
-- advanced query, sorting by correct date
Date id idCl nameCl opening dr closing
---------- ---------- ---------- ---------- ---------- ---------- ----------
2016-09-01 10L CL-J Jon 0 2000 2000
2016-09-01 10L CL-J Jon 2000 1000 3000
2016-09-22 20L CL-J Jon 3000 2000 5000
2016-09-05 20L CL-B Ben 0 3000 3000
2016-09-12 10L CL-B Ben 3000 8000 11000
2016-09-10 15.5L CL-A Alina 0 5000 5000
注意:
1)按时间顺序排列第二个查询顺序,用于输出本身和用于打开总和的行的排序
2)不使用整个表t1。例如,您是否只想处理与现有帐户匹配的条目,然后在插入中间表之前进行过滤" moneys"或者要使用的t2表条目
3)" id" (与" idCL"相比)没有明显的影响,匹配您想要的输出,例如Ben总计为11000.也许您想要总数超过所有" id"秒。 (起初我分开处理id,认为它们是不同的帐户,因为它们在t1中是分开的。)