zoukankan
html css js c++ java
同一张表内、利用变化的上一行数据动态地修改下一行数据(示例Demo)
--
同一张表内、利用变化的上一行数据动态地修改下一行数据(示例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
查看全文
相关阅读:
111
RH124-3 目录结构_转
oracle 查看表空间以及日志文件等系统文件
bash_profile
linux 7 关闭防火墙 开启sshd服务
mount 挂载光盘
oracle 夸服务器、数据库查询
Oracle中merge into的使用
restore和recover的区别
TCP: time wait bucket table overflow解决方法
原文地址:https://www.cnblogs.com/cyz1980/p/918477.html
最新文章
机器学习 | 算法笔记- K均值(K-Means)
Navicat for MySQL 设置定时任务(事件)
springboot有第三方jar打包成jar
Servlet入门总结及第一个Servlet程序
JDBC 插入时间字段的值
Mina TCP服务端客户端 示例
字节流、字符串、16进制字符串转换__java
mysql 按月统计但是有几个月没有数据,需要变成0
eclipse export runnable jar(导出可执行jar包)
PXE自动装机
热门文章
Mha-Atlas-MySQL高可用方案实践
Zabbix监控平台搭建部署与概述
Nginx反向代理与负载均衡
搭建FTP服务
DNS域名解析服务
LNMP分离式部署
DHCP服务
搭建一个jumpserver跳板机
系统安全及应用
SQL触发器中的inserted表和deleted表
Copyright © 2011-2022 走看看