关于记帐中的本期结存的查询方法 select *,结存=0 into #tfrom 表 adeclare @i intset @i=50update #t set @i=@i+isnull(本期进,0)-isnull(本期出,0),结存=@iselect * from #t 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 --测试--测试数据create table 表(编码 char(4),本期进 int,本期出 int)insert 表 select '0001',10 ,nullunion all select '0001',null,20union all select '0001',30 ,nullgo--查询处理select *,结存=0 into #tfrom 表 adeclare @i intset @i=50update #t set @i=@i+isnull(本期进,0)-isnull(本期出,0),结存=@iselect * from #tdrop table #tgo--删除测试drop table 表/*--测试结果编码 本期进 本期出 结存 ---- ----------- ----------- ----------- 0001 10 NULL 600001 NULL 20 400001 30 NULL 70(所影响的行数为 3 行)--*/ select 编码, 上期结余, sum(isnull(本期进,0)-isnull(本期出,0))+上期结余 as 结存 from tablenamegroup by 编码, 上期结余要输出你所要求的格式,还应该有个“发生时间”字段。 to victorycyz(中海):只得到合计数,只有一条记录啊to zjcxc(邹建) :谢谢!第一个答案是正确的,您没看走眼,其末结存是50,期初应是30难道只能用临时表吗?这样是不是速度慢!请教 sql访问速度问题 面试题:一个日期判断的sql语句? 一个可能很简单的问题,急救 sql查询遇到的问题 请大哥大姐帮忙,存储过程支持多字段排序后分页问题? syscolumns如何判断某个字段的类型? 简单问题,答者有分! 数据库数值置空的问题 是否有人在REDHAT7。1上安装成功ORACLE8。16 FOR LINUX!!!! sql server 2008 报表开发问题 比较急: 如何处理备份文件恢复时的排序规则冲突问题? 急啊, 我誤刪了表里的東西,有沒有辦法恢復
create table 表(编码 char(4),本期进 int,本期出 int)
insert 表 select '0001',10 ,null
union all select '0001',null,20
union all select '0001',30 ,null
go--查询处理
select *,结存=0 into #t
from 表 adeclare @i int
set @i=50
update #t set @i=@i+isnull(本期进,0)-isnull(本期出,0),结存=@iselect * from #tdrop table #t
go--删除测试
drop table 表/*--测试结果
编码 本期进 本期出 结存
---- ----------- ----------- -----------
0001 10 NULL 60
0001 NULL 20 40
0001 30 NULL 70(所影响的行数为 3 行)
--*/
select 编码,
上期结余,
sum(isnull(本期进,0)-isnull(本期出,0))+上期结余 as 结存
from tablename
group by 编码, 上期结余
要输出你所要求的格式,还应该有个“发生时间”字段。
只得到合计数,只有一条记录啊
to zjcxc(邹建) :
谢谢!
第一个答案是正确的,您没看走眼,其末结存是50,期初应是30
难道只能用临时表吗?这样是不是速度慢!
请教