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

更新中間行

Update intermediate rows(更新中間行)
本文介紹了更新中間行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個這樣的數據集:

Hi I have a data set like this:

Date                    ID
2015-06-17 15:57:00.000 1
NULL                    2
NULL                    3
NULL                    4
NULL                    5
NULL                    6
2015-06-17 15:58:00.000 7
NULL                    8
NULL                    9
NULL                    10
NULL                    11
NULL                    12
2015-06-17 17:50:04.000 13
NULL                    14
2015-06-17 17:51:00.000 16
NULL                    17
2015-06-17 17:52:03.000 19
NULL                    20
2015-06-17 17:52:04.000 22
NULL                    23
2015-06-17 17:52:04.000 25
NULL                    26
2015-06-17 17:52:04.000 28
NULL                    29

如您所見,它們是按順序排列的(升序),但許多日期為 NULL

As you can see, they are in sequence (ascending), but many dates are NULL

我想更新 NULL 條目以獲得最近的先前日期/時間

I want to update the NULL entries to have the nearest prior date/time

所以第 2 到第 6 行應該從第 1 行和第 1 行獲取日期時間8 到 12 應該從 ID 7 等獲取日期時間.

So rows 2 thru 6 should get the date time from row ID 1 and 8 thru 12 should get datetime from ID 7, etc.

我確信有一個簡單的方法可以通過一個更新語句來做到這一點,但我會畫一個空白

I'm sure there's an easy way to do this with a single update statement, but I'd drawing a blank

推薦答案

您應該能夠使用相關子查詢來做到這一點;當我嘗試時,以下查詢似乎有效(請參閱下面的小提琴),但請務必準備好備用以防萬一我錯了:-)

You should be able to use a correlated subquery to do this; the following query seemed to work when I tried it (see fiddle below), but be sure to have a backup handy in case I'm wrong :-)

update t1
set date = (select max(date) from your_table where id <= t1.id and date is not null)
from your_table t1
where t1.date is null

示例 SQL 小提琴

請注意,如果日期不是我假設的順序,這可能會產生一些奇怪的結果.

Note that this might give some strange results if the dates aren't sequential which I assumed they are.

此外,如果您使用的是 SQL Server 2012+ 版本,則使用 max() 函數作為窗口函數(max(date) over (...)) 是更好的選擇.詳細信息在另一個答案中提供.

Also, if you're using a version 2012+ of SQL Server then using the max() function as a windowed function (max(date) over (...)) is a better option. The details are presented in another answer.

這篇關于更新中間行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
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?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 国产伦精品一区二区 | 欧美一区二区三区在线播放 | 色婷婷一区二区三区四区 | 亚洲午夜精品 | 四虎影院免费在线 | 成人18亚洲xxoo | 91精品国产麻豆 | 久草久草久草 | 成人教育av | 国产一卡二卡三卡 | 欧美中文字幕在线观看 | 青娱乐一区二区 | 中文字幕精品一区二区三区精品 | 久在线 | 亚洲国产一区二区在线 | 亚洲精品成人 | 国产99久久精品 | 97精品超碰一区二区三区 | 亚洲风情在线观看 | 中文一区二区 | 国产三级 | 午夜视频导航 | 亚洲九九精品 | 日韩久久成人 | 亚洲国产免费 | 精品亚洲永久免费精品 | 欧美三区视频 | 91精品久久久久久久久中文字幕 | 成人黄色网址大全 | a视频在线观看 | 日韩久久中文字幕 | 看片一区| 成av在线| 中文字幕高清视频 | 特一级毛片 | av在线一区二区三区 | 欧美一级毛片久久99精品蜜桃 | 欧美日日日日bbbbb视频 | 亚洲国产网址 | 国产真实精品久久二三区 | 黄色一级免费看 |