高手救命:做一个应收明细表,已经知道期初金额,有一张表是发生额,如何求出期末金额

放飞阳光 2010-04-24 07:13:12
期初金额:1000
发生额表:
序号 提货金额 期末金额
1 10 990
2 20 970
3 30 940
4 10 930


用sql 如何做,谢谢!

...全文
129 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
放飞阳光 2010-04-24
  • 打赏
  • 举报
回复
你们太历害了,非常感谢
feixianxxx 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gxabc001 的回复:]

前面忘记加回款金额了, 回款的也要算到期末
[/Quote]
kan xia 6L合适否
dawugui 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 gxabc001 的回复:]
我的表情况是这样,前面的不对
期初金额:1000
发生额表:
序号 提货金额 回款金额 期末金额 类型
1 10 990 提货
2 20 970 提货
3 30 940 提货
4 10 930 提货
5 20 950 回款
6 10 960 提货
[/Quote]
create table tb(序号 int,提货金额 int,期末金额 int,类型 varchar(10))
insert into tb values(1 ,10 ,0,'提货')
insert into tb values(2 ,20 ,0,'提货')
insert into tb values(3 ,30 ,0,'提货')
insert into tb values(4 ,10 ,0,'提货')
insert into tb values(5 ,20 ,0,'回款')
insert into tb values(6 ,10 ,0,'提货')

go

declare @je as int
set @je = 1000

select 序号 ,提货金额 ,类型, 期末金额 = @je - (select sum(case when 类型 = '提货' then 提货金额 else -提货金额 end) from tb where 序号 <= t.序号) from tb t

drop table tb

/*

序号 提货金额 类型 期末金额
----------- ----------- ---------- -----------
1 10 提货 990
2 20 提货 970
3 30 提货 940
4 10 提货 930
5 20 回款 950
6 10 提货 940

(所影响的行数为 6 行)

*/

--小F-- 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 feixianxxx 的回复:]
SQL code
declare @cq as int
set @cq = 1000

select t.序号 ,t.提货金额 , 期末金额 =1000-SUM(k.提货金额)
from tb t join tb k on k.序号 <= t.序号
group by t.序号 ,t.提货金额
order by t.序号
/*
序号 提货金额 期……
[/Quote]
...
放飞阳光 2010-04-24
  • 打赏
  • 举报
回复
前面忘记加回款金额了, 回款的也要算到期末
feixianxxx 2010-04-24
  • 打赏
  • 举报
回复
create table tb(序号 int,提货金额 int,期末金额 int,类型 varchar(10))
insert into tb values(1 ,10 ,0,'提货')
insert into tb values(2 ,20 ,0,'提货')
insert into tb values(3 ,30 ,0,'提货')
insert into tb values(4 ,10 ,0,'提货')
insert into tb values(5 ,20 ,0,'回款')
insert into tb values(6 ,10 ,0,'提货')
go
declare @cq as int
set @cq = 1000

select t.序号 ,t.提货金额 , 期末金额 =1000+SUM(case when k.类型='提货' then -k.提货金额 else k.提货金额 end)
from tb t join tb k on k.序号 <= t.序号
group by t.序号 ,t.提货金额
order by t.序号

drop table tb
/*
序号 提货金额 期末金额
----------- ----------- -----------
1 10 990
2 20 970
3 30 940
4 10 930
5 20 950
6 10 940*/

LZ最后一行结果有误
放飞阳光 2010-04-24
  • 打赏
  • 举报
回复
/*
序号 提货金额 回款金额 期末金额
----------- ----------- ----------- -----------
1 10 990
2 20 970
3 30 940
4 10 930
5 20 950
*/
feixianxxx 2010-04-24
  • 打赏
  • 举报
回复
declare @cq as int
set @cq = 1000

select t.序号 ,t.提货金额 , 期末金额 =1000-SUM(k.提货金额)
from tb t join tb k on k.序号 <= t.序号
group by t.序号 ,t.提货金额
order by t.序号
/*
序号 提货金额 期末金额
----------- ----------- -----------
1 10 990
2 20 970
3 30 940
4 10 930*/
放飞阳光 2010-04-24
  • 打赏
  • 举报
回复
我的表情况是这样,前面的不对
期初金额:1000
发生额表:
序号 提货金额 回款金额 期末金额 类型
1 10 990 提货
2 20 970 提货
3 30 940 提货
4 10 930 提货
5 20 950 回款
6 10 960 提货
feixianxxx 2010-04-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dawugui 的回复:]

SQL code
create table tb(序号 int,提货金额 int,期末金额 int)
insert into tb values(1 ,10 ,0)
insert into tb values(2 ,20 ,0)
insert into tb values(3 ,30 ,0)
insert into tb values(4 ,10 ,0)
go

declare @je as ……
[/Quote]
...
dawugui 2010-04-24
  • 打赏
  • 举报
回复
create table tb(序号 int,提货金额 int,期末金额 int)
insert into tb values(1 ,10 ,0)
insert into tb values(2 ,20 ,0)
insert into tb values(3 ,30 ,0)
insert into tb values(4 ,10 ,0)
go

declare @je as int
set @je = 1000

select 序号 ,提货金额 , 期末金额 = @je - (select sum(提货金额) from tb where 序号 <= t.序号) from tb t

drop table tb

/*

序号 提货金额 期末金额
----------- ----------- -----------
1 10 990
2 20 970
3 30 940
4 10 930

(所影响的行数为 4 行)


*/

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧