T-SQL 将当前行更新到下一行的值问题

T-SQL Updating current row to next row#39;s value issue(T-SQL 将当前行更新到下一行的值问题)
本文介绍了T-SQL 将当前行更新到下一行的值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一些关于在特定场景中哪种更新最适合 t-sql DML 的建议.

I am looking for some advice on what type of update would be the best in a certain scenario for t-sql DML.

遇到数据问题,结束日期比当前记录的开始日期早一天,并且需要将多个实体的结束日期设置为下一行的开始日期

came into a data issue where end dates are one day before the start date on a current record, and need to set the end date to the start date of the next in row for multiple entities

例如

rowid     entity    id  record_type start_date  end_date
214       250       1   H           2015-01-01  2014-12-31
329       250       1   H           2015-04-25  2015-04-24
533       250       1   C           2015-11-01  NULL
11        250       2   H           2015-06-01  2014-05-29
292       250       2   H           2015-09-11  2015-09-10
987       250       2   C           2015-10-01  NULL

我需要做的是将第一条记录的 end_date 更新为下一条记录的 startdate - 每个员工/实体都为 1.

What I need to do is update the first record end_date to the next record startdate - 1 on each employee/entity.

目前,受此影响的实体超过 5000 个,因此我试图以某种方式在每条记录上更新此信息,以节省时间.

Currently, there are over 5K entities affected by this, so I am trying to get this updated on each record somehow to save time on this.

我能做但要花很多时间的就是1. 将所有公司的历史记录的最大行数合并为一个数2.为总行数创建相同数量的临时表3. 将最小开始日期值插入到第一个临时表中4.将不在临时表1中的最小值插入到表2中,依此类推5. 然后将临时表 1 的结束日期更新为临时表 2 的开始日期 -1 天6. 从那里,对实际表运行更新,为每个临时表运行多个更新语句,在 rowid 上加入.

What I can do but is taking a lot of time, is to 1. get a max rowcount of history records for all companies into one number 2. create the same amount of temp tables for the number of total rows 3. insert minimum start date values into first temp table 4. insert minimum value not in temp table 1 into table 2, and so on 5. then update temp table 1's end date to temptable 2's startdate -1 day 6. from there, run an update on the actual table with multiple update statements for each temp table, joined on rowid.

最终输出如下:

rowid     entity    id  record_type start_date  end_date
214       250       1   H           2015-01-01  2014-04-24
329       250       1   H           2015-04-25  2015-10-31
533       250       1   C           2015-11-01  NULL
11        250       2   H           2015-06-01  2014-09-10
292       250       2   H           2015-09-11  2015-9-31
987       250       2   C           2015-10-01  NULL

除了我的一长串临时表/更新之外的任何建议将不胜感激!我在想一些可能是光标的东西,但我不太确定这是否是为这种情况编写更新的更快方法.

Any suggestions besides my long list of temp tables/updates would be greatly appreciated! I was thinking something along the lines of possibly a cursor, but I am not too sure if this would be a quicker way of writing an update for this scenario.

推荐答案

我认为可更新的 CTE 是要走的路.在 SQL Server 2012+ 中,您可以使用 lead():

I think updatable CTEs are the way to go. In SQL Server 2012+, you can use lead():

with toupdate as (
      select t.*,
             lead(start_date) over (partition by entity order by start_date) as next_start_date
      from t
     )
update toupdate
     set end_date = dateadd(day, -1, next_start_date)
     where end_date = dateadd(day, -1, start_date);

这篇关于T-SQL 将当前行更新到下一行的值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Creating table with T-SQL - can#39;t see created tables in Object explorer(使用 T-SQL 创建表 - 在对象资源管理器中看不到创建的表)
How to check if VARCHAR strings are (not) hexadecimal?(如何检查 VARCHAR 字符串是否为(非)十六进制?)
Arithmetic overflow error converting IDENTITY to data type int(将 IDENTITY 转换为数据类型 int 的算术溢出错误)
Where clause if there are multiple of the same ID(如果有多个相同的 ID,Where 子句)
Azure SQL: Invalid Object Name using Powershell#39;s quot;Invoke-sqlcmdquot; on Adventureworks(Azure SQL:使用 Powershell 的“Invoke-sqlcmd的无效对象名称在 Adventureworks 上)
How to Convert Table Data into xml format using sql with multiple sub nodes(如何使用具有多个子节点的sql将表数据转换为xml格式)