sql server 使用游标,循环将上一行的字段求和,更新到下一行

原始表数据如下:
表名:score
下一行的sid=上一行的sid+score

代码实现如下:

--定义游标(可以是动态,也可以是静态)
DECLARE my_cursor  CURSOR  STATIC LOCAL FOR 
SELECT * FROM dbo.score ORDER BY id --依赖id排序
OPEN my_cursor
DECLARE @id TINYINT,
@sid INT,
@sidL INT, --声明上一行的sid
@score INT,
@scoreL INT,--声明上一行的score
@index INT=1 --声明index ,利用index变化找到上下行的值,设置为1 
FETCH NEXT   FROM my_cursor INTO @id,@sid,@score
WHILE @@FETCH_STATUS=0
BEGIN
	SET @sidL=(SELECT sid FROM dbo.score WHERE id=@index)
	SET @scoreL=(SELECT score FROM dbo.score WHERE id=@index)
	SET @index=@index+1 --上一行查出来后,将index指向下一行
	UPDATE dbo.score SET sid=@sidL+@scoreL WHERE id=@index --更新下一行
	FETCH ABSOLUTE @index FROM my_cursor INTO @id,@sid,@score --游标调到下一行
END
CLOSE my_cursor
DEALLOCATE my_cursor
SELECT * FROM dbo.score

结果:
在这里插入图片描述

踩过的坑:
必须用select 查询历史值实现,不知道为什么,一开始使用动态游标用fetch next ,发现fetch next 读取不到update 后的sid,始终下一行sid 为空。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import sys sys.tracebacklimit = 0 import os os.environ['PYTHONUNBUFFERED'] = '1' import arcpy 获取参数 input_features = arcpy.GetParameterAsText(0) join_field = arcpy.GetParameterAsText(1) target_feature = arcpy.GetParameterAsText(2) target_field = arcpy.GetParameterAsText(3) area_threshold = arcpy.GetParameterAsText(4) 创建空间连接 join_result = arcpy.SpatialJoin_analysis(input_features, target_feature, "in_memory/spatial_join", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT") 使用MakeFeatureLayer创建要素图层,并使用AddFieldDelimiters处理字段名称 join_layer = arcpy.management.MakeFeatureLayer(join_result, "join_layer").getOutput(0) join_field_name = arcpy.AddFieldDelimiters(join_layer, join_field) 使用SelectLayerByAttribute选择重叠面积大于阈值的要素 arcpy.management.SelectLayerByAttribute(join_layer, "NEW_SELECTION", "Shape_Area > " + str(area_threshold)) 使用SummaryStatistics工具进行面积求和 summary_table = arcpy.Statistics_analysis(join_layer, "in_memory/summary_table", [["Shape_Area", "SUM"]], join_field_name) 使用TableToNumPyArray将结果转换为字典 sum_dict = {} with arcpy.da.TableToNumPyArray(summary_table, [join_field, "SUM_Shape_Area"]) as arr: for row in arr: sum_dict[row[0]] = row 使用UpdateCursor更新目标要素类的目标字段 with arcpy.da.UpdateCursor(target_feature, [target_field, join_field], sql_clause=(None, "ORDER BY OBJECTID")) as cursor: for row in cursor: join_value = row[1] if join_value in sum_dict: area_sum = sum_dict[join_value] row[0] = area_sum cursor.updateRow(row) 导出结果 output_feature = arcpy.GetParameterAsText(5) arcpy.CopyFeatures_management(target_feature, output_feature) 删除游标对象和要素图层对象 del cursor, join_layer请改正为可复制代码
05-24
import sys import os import arcpy # 禁用traceback,避免出现异常时输出堆栈信息 sys.tracebacklimit = 0 # 设置PYTHONUNBUFFERED环境变量,避免使用arcpy输出信息时出现延迟 os.environ['PYTHONUNBUFFERED'] = '1' # 获取参数 input_features = arcpy.GetParameterAsText(0) join_field = arcpy.GetParameterAsText(1) target_feature = arcpy.GetParameterAsText(2) target_field = arcpy.GetParameterAsText(3) area_threshold = arcpy.GetParameterAsText(4) # 创建空间连接 join_result = arcpy.SpatialJoin_analysis(input_features, target_feature, "in_memory/spatial_join", "JOIN_ONE_TO_ONE", "KEEP_ALL", "", "INTERSECT") # 使用MakeFeatureLayer创建要素图层,并使用AddFieldDelimiters处理字段名称 join_layer = arcpy.management.MakeFeatureLayer(join_result, "join_layer").getOutput(0) join_field_name = arcpy.AddFieldDelimiters(join_layer, join_field) # 使用SelectLayerByAttribute选择重叠面积大于阈值的要素 arcpy.management.SelectLayerByAttribute(join_layer, "NEW_SELECTION", "Shape_Area > " + str(area_threshold)) # 使用SummaryStatistics工具进行面积求和 summary_table = arcpy.Statistics_analysis(join_layer, "in_memory/summary_table", [["Shape_Area", "SUM"]], join_field_name) # 使用TableToNumPyArray将结果转换为字典 sum_dict = {} with arcpy.da.TableToNumPyArray(summary_table, [join_field, "SUM_Shape_Area"]) as arr: for row in arr: sum_dict[row[0]] = row # 使用UpdateCursor更新目标要素类的目标字段 with arcpy.da.UpdateCursor(target_feature, [target_field, join_field], sql_clause=(None, "ORDER BY OBJECTID")) as cursor: for row in cursor: join_value = row[1] if join_value in sum_dict: area_sum = sum_dict[join_value]["SUM_Shape_Area"] row[0] = area_sum cursor.updateRow(row) # 导出结果 output_feature = arcpy.GetParameterAsText(5) arcpy.CopyFeatures_management(target_feature, output_feature) # 删除游标对象和要素图层对象 del cursor, join_layer

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值