select
期初库存=isnull((select sum(发生数量) from 表 where 发生时间<a.发生时间),0),
a.发生业务,
a.发生数量,
a.发生时间,
期末库存=(select sum(发生数量) from 表 where 发生时间<=a.发生时间)
from
表 a
order by
a.发生时间
期初库存=isnull((select sum(发生数量) from 表 where 发生时间<a.发生时间),0),
a.发生业务,
a.发生数量,
a.发生时间,
期末库存=(select sum(发生数量) from 表 where 发生时间<=a.发生时间)
from
表 a
order by
a.发生时间
解决方案 »
- 为什么这个数据库附加不上!
- sql 查询
- 装SQL Server2005的机器上能否安装SQL Server2000
- 请教大虾们一个菜菜的问题,多谢
- 未能从程序集“Microsoft.SqlServer.Smo,Version=9.0.242.0,Culture=neutral,PublicKeyTok
- 急,SQL2005配置镜像完成,测试问题
- 关于用户DNS与系统DNS
- Sql CE数据库的一个数据表最多可以放多少条数据?
- 急啊,在线等!关于动态SQLINSERT的写法
- sql server有没有oracle中connect by 相应的语句?????
- 如何将dts包的结构化存储文件通过语句的转化为数据库存储模式啊!
- 用sql语句能不能获得硬盘号?????精神有些不正常了????help me
期初库存=isnull((select sum(发生数量) from 表 where id<a.id),0),
a.发生业务,
a.发生数量,
a.发生时间,
期末库存=(select sum(发生数量) from 表 where id<=a.id)
from
表 a
order by
a.id---思路不变,加一个id列,如果发生时间是一个完整的时间也不会有问题的
试过了,还是不行
set nocount on
declare @a table(发生业务 varchar(10),发生数量 int,发生时间 char(10))insert @a select '进货',10 ,'2003/02/05'
insert @a select '销售',-5 ,'2003/02/08'
insert @a select '进货',20 ,'2003/02/10'
insert @a select '进货',5 ,'2003/02/15'
insert @a select '销售',-15,'2003/02/18'declare @b table(id int identity(1,1),期初库存 int,发生业务 varchar(10),发生数量 int,发生时间 char(10),期末库存 int)
insert @b(发生业务,发生数量,发生时间) select 发生业务,发生数量,发生时间 from @a order by 发生时间
update @b set 期初库存=0,期末库存=发生数量 where id=1
update @b set 期初库存=(select 期末库存 from @b where id=1) where id=2declare @i int
set @i=2
while @i<=(select max(id) from @b) begin
update @b set 期末库存=期初库存+发生数量 where id=@i
update @b set 期初库存=(select 期末库存 from @b where id=@i) where id=@i+1
set @i=@i+1
end
set nocount off
select 期初库存,发生业务,发生数量,发生时间,期末库存 from @b/*结果
期初库存 发生业务 发生数量 发生时间 期末库存
----------- ---------- ----------- ---------- -----------
0 进货 10 2003/02/05 10
10 销售 -5 2003/02/08 5
5 进货 20 2003/02/10 25
25 进货 5 2003/02/15 30
30 销售 -15 2003/02/18 15(所影响的行数为 5 行)
*/
期初库存=isnull((select sum(发生数量) from #T where 发生时间<=a.发生时间 and id<a.id),0),
a.发生业务,
a.发生数量,
a.发生时间,
期末库存=(select sum(发生数量) from #T where 发生时间<=a.发生时间 and id<=a.id)
from
#T a
order by
a.发生时间drop table #T
=========================加一个id列,如果发生时间是一个完整的时间也不会有问题的,为什么???????????????
set nocount on
declare @a table(发生业务 varchar(10),发生数量 int,发生时间 char(20))insert @a select '进货',10 ,'2003-02-05'
insert @a select '销售',-5 ,'2003-02-08 15:15:00'
insert @a select '进货',20 ,'2003-02-08 09:00:00'
insert @a select '进货',5 ,'2003-02-08 12:00:00'
insert @a select '销售',-15,'2003-02-18'select identity(int,1,1) as id,* into #T from @a order by 发生时间select
期初库存=isnull((select sum(发生数量) from #T where 发生时间<=a.发生时间 and id<a.id),0),
a.发生业务,
a.发生数量,
a.发生时间,
期末库存=(select sum(发生数量) from #T where 发生时间<=a.发生时间 and id<=a.id)
from
#T a
order by
a.发生时间drop table #T