同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
来源:互联网 发布:c语言统计每个字母个数 编辑:程序博客网 时间:2024/05/30 04:31
--同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
/**//*
CREATE TABLE Account (
[Month] [datetime] NOT NULL Prmary Key,
[PlanFare] [money] NULL ,
[PutoutFare] [money] NULL ,
[PlanPreFare] [money] NULL ,
[PutoutPreFare] [money] NULL
) --Account表生成脚本
*/
select identity(int,1,1) as [id],* into # from account order by [month] asc
CREATE TABLE #Temp (
[id] [int] NOT NULL,
[Month] [datetime] NOT NULL ,
[PlanFare] [money] NULL ,
[PutoutFare] [money] NULL ,
[PlanPreFare] [money] NULL ,
[PutoutPreFare] [money] NULL
)
declare @id int
declare m cursor for select [id] from # order by [id] asc
open m
fetch next from m into @id
while (@@fetch_status=0)
begin
insert into #Temp([id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare)
select [id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare from # where [id]=@id
UPDATE #
SET #.PlanPreFare = (a.PlanFare - a.putoutFare) + (a.PlanPreFare - a.putoutPreFare)
FROM #Temp AS a
WHERE #.[id] = a.[id]+1
--前行作依据,后行来修改(此次的后行,变成下次的前行,依此循环,直至表中的行遍历结束)
truncate table #Temp
fetch next from m into @id
end
close m
deallocate m
UPDATE Account
SET Account.PlanPreFare = a.PlanPreFare
FROM # AS a
WHERE Account.[Month] = a.[Month]
drop table #
drop table #Temp
/**//*
CREATE TABLE Account (
[Month] [datetime] NOT NULL Prmary Key,
[PlanFare] [money] NULL ,
[PutoutFare] [money] NULL ,
[PlanPreFare] [money] NULL ,
[PutoutPreFare] [money] NULL
) --Account表生成脚本
*/
select identity(int,1,1) as [id],* into # from account order by [month] asc
CREATE TABLE #Temp (
[id] [int] NOT NULL,
[Month] [datetime] NOT NULL ,
[PlanFare] [money] NULL ,
[PutoutFare] [money] NULL ,
[PlanPreFare] [money] NULL ,
[PutoutPreFare] [money] NULL
)
declare @id int
declare m cursor for select [id] from # order by [id] asc
open m
fetch next from m into @id
while (@@fetch_status=0)
begin
insert into #Temp([id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare)
select [id],[Month], PlanFare, PutoutFare, PlanPreFare, PutoutPreFare from # where [id]=@id
UPDATE #
SET #.PlanPreFare = (a.PlanFare - a.putoutFare) + (a.PlanPreFare - a.putoutPreFare)
FROM #Temp AS a
WHERE #.[id] = a.[id]+1
--前行作依据,后行来修改(此次的后行,变成下次的前行,依此循环,直至表中的行遍历结束)
truncate table #Temp
fetch next from m into @id
end
close m
deallocate m
UPDATE Account
SET Account.PlanPreFare = a.PlanPreFare
FROM # AS a
WHERE Account.[Month] = a.[Month]
drop table #
drop table #Temp
- 同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
- 修改一行数据的锁生命周期
- jsf dataTable修改一行数据的方法
- SQL中每一行的数据和上一行的数据进行计算
- 利用sstream分割txt文件的一行数据
- js实现简单的动态添加或删除一行数据
- 利用强类型DataSet插入一行数据
- finereport 学习循环引用 获取列表的上一行数据。
- 用Java修改文件最后一行数据
- jquery动态添加删除一行数据
- 01-复制上一行到下一行代码的快捷键
- 获取DataTabl的第一行和最后一行的数据
- ItemDataBound时方便地得到绑定的数据集的一行
- DataTable获取第一行和最后一行的数据
- 把一行里 ‘,’ 隔开的数据一行转换成多行
- 如何用SQL语句把同一列的数据按某个group by语句组合成一行数据
- 同一id下的其它列,多行变一行
- 添加可动态变化的输入框,点击添加增加一行,点击删除删除一行
- [求助] TOmcat上下文丢失!
- 搜索三本书
- 关于Xerces-C++执行schema校验相关问题的详细解答
- 屏蔽关闭按钮
- 罗伯特.巴尼台球教程
- 同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
- VC++编译选项
- 教你如何挂马
- 精妙的SQL语句(值得收藏)
- 堆内存和栈内存详解
- 向依赖关系宣战
- Oracle 日期处理完全版
- 推荐一个能快速启动程序的工具
- ESB架构笔记