久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

T-SQL 將當(dāng)前行更新到下一行的值問(wèn)題

T-SQL Updating current row to next row#39;s value issue(T-SQL 將當(dāng)前行更新到下一行的值問(wèn)題)
本文介紹了T-SQL 將當(dāng)前行更新到下一行的值問(wèn)題的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在尋找一些關(guān)于在特定場(chǎng)景中哪種更新最適合 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.

遇到數(shù)據(jù)問(wèn)題,結(jié)束日期比當(dāng)前記錄的開(kāi)始日期早一天,并且需要將多個(gè)實(shí)體的結(jié)束日期設(shè)置為下一行的開(kāi)始日期

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 - 每個(gè)員工/實(shí)體都為 1.

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

目前,受此影響的實(shí)體超過(guò) 5000 個(gè),因此我試圖以某種方式在每條記錄上更新此信息,以節(jié)省時(shí)間.

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.

我能做但要花很多時(shí)間的就是1. 將所有公司的歷史記錄的最大行數(shù)合并為一個(gè)數(shù)2.為總行數(shù)創(chuàng)建相同數(shù)量的臨時(shí)表3. 將最小開(kāi)始日期值插入到第一個(gè)臨時(shí)表中4.將不在臨時(shí)表1中的最小值插入到表2中,依此類(lèi)推5. 然后將臨時(shí)表 1 的結(jié)束日期更新為臨時(shí)表 2 的開(kāi)始日期 -1 天6. 從那里,對(duì)實(shí)際表運(yùn)行更新,為每個(gè)臨時(shí)表運(yùn)行多個(gè)更新語(yǔ)句,在 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

除了我的一長(zhǎng)串臨時(shí)表/更新之外的任何建議將不勝感激!我在想一些可能是光標(biāo)的東西,但我不太確定這是否是為這種情況編寫(xiě)更新的更快方法.

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.

推薦答案

我認(rèn)為可更新的 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);

這篇關(guān)于T-SQL 將當(dāng)前行更新到下一行的值問(wèn)題的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個(gè)子標(biāo)記轉(zhuǎn)換為具有多個(gè)分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個(gè)表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計(jì)算值創(chuàng)建計(jì)算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對(duì)?) - IT屋-程序員軟件開(kāi)發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱(chēng)轉(zhuǎn)換為日期/月份編號(hào)(問(wèn)題和答案的組合))
主站蜘蛛池模板: 黄色大片免费观看 | 欧美精品欧美精品系列 | 久久综合影院 | 国产日韩欧美电影 | 91精品中文字幕一区二区三区 | 天天夜干| 欧美日韩国产一区二区三区 | 羞羞视频一区二区 | 网站国产 | 性色网站 | 天堂精品 | 国产精品99久久久久久www | 黄网免费 | 久久久久久艹 | 99久久免费精品国产男女高不卡 | 午夜91| 午夜在线 | 日韩久久在线 | 亚洲第一成人av | 97超在线视频 | 欧美三级网站 | 一区二区三区不卡视频 | 人人干人人干人人 | 九九热精品免费 | 亚洲欧美一区二区三区在线 | 亚洲高清在线 | 久久久成人网 | 噜久寡妇噜噜久久寡妇 | 国产免费福利小视频 | 国产成人免费视频 | 亚洲天堂免费在线 | 欧美精品v国产精品v日韩精品 | 久久免费国产 | 国产精品美女久久久久久久网站 | 在线视频亚洲 | 精品久久1 | 香蕉久久久| 午夜激情视频 | 欧美一区二区三区大片 | 欧美日韩福利视频 | 精品久久成人 |