多条单据,每个单据多条明细,循环时,判断出每个单据的开始
2015-08-28 11:47
267 查看
适用多条单据,每个单据多条明细,要对每个单据的明细,进行业务处理。循环数据,识别出每个单据的第一条明细。
--create table ABC --( -- A int, -- B int, -- C int --) --insert ABC select '1','20',null --insert ABC select '1','10',null --insert ABC select '2','15',null --insert ABC select '2','5',null if exists(select * from sysobjects where id=object_id(N'tempdb..#ttt') ) drop table #ttt DECLARE @sid INT,@sno INT,@preid int,@curid int,@ccid int,@ctotal int select IDENTITY(int,1,1) as fid,* into #ttt from ABC order by A asc, B desc SET @sno=@@rowcount SET @sid=1 set @preid=0 set @ctotal=0 WHILE @sid<=@sno BEGIN select @curid=A from #ttt where fid=@sid select @ccid=B from #ttt where fid=@sid if @preid<>@curid begin set @ctotal=@ccid set @preid=@curid end else begin set @ctotal=@ctotal-@ccid end update #ttt set c=@ctotal where fid=@sid SET @sid=@sid+1 END select * from #ttt drop table #ttt
fid | A | B | C |
1 | 1 | 20 | 20 |
2 | 1 | 10 | 10 |
3 | 2 | 15 | 15 |
4 | 2 | 5 | 10 |
相关文章推荐
- POJ3411--Paid Roads
- jquery获取具有多个类class的标签内容
- 转 9个基于java的搜索引擎框架
- JS动态修改页面EasyUI datebox不生效、EasyUI动态添加Class、EasyUI动态渲染解析解决方案
- sql用法
- VO , PO , BO , QO, DAO ,POJO
- *Sum root to leaf number
- thinkphp U方法 跨入口生成连接
- External Storage外部存储——SD卡
- PHP trigger_error() 函数
- hdoj 2036 改革春风吹满地
- Socket编程(摘抄)
- xshell5连接centos6.7时提示服务器拒绝了密码的解决办法
- iOS开发之多图片无缝滚动组件封装与使用
- 机器学习——深度学习(Deep Learning)经典资料
- sec-wiki
- SVN服务器搭建和使用(三)
- [LeetCode] 238 - Product of Array Except Self
- [转万一] 不使用标题栏拖动窗体
- HDOJ1181(简单DFS)(练习使用STL)