目前工作需要每日从系统内导入数百科目的余额,但是增量系统不给,需要自己计算,因此想请教一下如何计算:
数据表内容如下:
科目 日期 余额
A 2018.1.1 10
B 2018.1.1 5
C 2018.1.1 7
A 2018.1.2 10
B 2018.1.2 15
C 2018.1.2 17
A 2018.1.3 10
B 2018.1.3 25
C 2018.1.3 27
如何能够得到下表
科目 时间 增量
A 2018.1.1 10
B 2018.1.1 5
C 2018.1.1 7
A 2018.1.2 0
B 2018.1.2 10
C 2018.1.2 10
A 2018.1.3 0
B 2018.1.3 10
C 2018.1.3 10
试过开窗函数,结果发现ACCESS不支持
sql语句搞定, select A.科目,A.时间,(A.量-B.量) as 增量 from A join in B Where A.科目=B.科目 and A.时间 = B.时间
需要时查找一个
select tt1.科目, tt1.时间, (tt1.增量-tt2.增量) from table as tt1,table as tt2 where tt1.科目 = 'A' and tt1.时间 = '2018.1.2' and tt2.科目 = 'A' and tt2.时间 = '2018.1.1'
create table testtable_csdn180723_b (id int identity(1,1),k varchar(30),d date,y int)
select k,d,y-isnull((select top 1 y from testtable_csdn180723_b where d<tb1.d and k=tb1.k order by d desc),0) diff_y from testtable_csdn180723_b tb1
(https://img-ask.csdn.net/upload/201808/03/1533310523_975597.jpg)
select * from aa where a2 in(select min(a2) from aa) union select b.a1,b.a2,b.a3-a.a3 from aa a ,aa b Where A.a1=B.a1 and b.a2-a.a2=1 into table Inquire
2、按时间排序
select * from Inquire order by a2
1、源数据表aa:
2、创建查询表 Inquire1、
select * from aa where a2 in(select min(a2) from aa) union select b.a1,b.a2,b.a3-a.a3 from aa a ,aa b Where A.a1=B.a1 and b.a2-a.a2=1 into table Inquire
3、按时间排序
select * from Inquire order by a2