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

通過在 BI_StartDate 中添加一天避免重疊

Avoid overlapping in by adding a day in BI_StartDate(通過在 BI_StartDate 中添加一天避免重疊)
本文介紹了通過在 BI_StartDate 中添加一天避免重疊的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下員工版本控制表:

I have the following Employee versioning table:

EmployeeId ManagerId DepartmentId StartDate   EndDate
--------------------------------------------------------
45         2         56           2017-06-27  2018-02-07
45         3         98           2018-02-07  2018-08-25
45         3         55           2018-02-25  2018-08-25
45         6         44           2018-08-25  9999-12-31

我想更正 StartDate 如下以避免重疊:

I want to correct StartDate as below to avoid the overlap as below:

EmployeeId ManagerId DepartmentId StartDate   EndDate
---------------------------------------------------------
45         2         56           2017-06-27  2018-02-07
45         3         98           2018-02-08  2018-08-25
45         3         55           2018-02-26  2018-08-26
45         6         44           2018-08-27  9999-12-31

第一條記錄的邏輯如下EndDate = 2018-02-07,下一條記錄將有EndDate + 1 day = 2018-02-08.對于 EndDate = StartDate 的記錄,它將在前一個 EndDate + 1 中都有.

The logic is like below for the first record the EndDate = 2018-02-07, the next record will have EndDate + 1 day = 2018-02-08. For the record with EndDate = StartDate it will have in both The previous EndDate + 1.

推薦答案

假設你的數據不是很離譜,可以使用lag():

Assuming your data is not very off, you can use lag():

with toupdate as (
      select t.*,
             lag(enddate) over (partition by employee order by startdate) as prev_enddate
      from t
     ) t
update toupdate
    set startdate = dateadd(day, 1, prev_enddate)
    where startdate <> dateadd(day, 1, prev_enddate);

如果您的數據有很多非常復雜的重疊,那么這會變得有點棘手.基本上,您希望保留結束日期并使用它們來計算開始日期——除了第一行之外的所有日期:

If your data has lots of really complicated overlaps, then this gets a bit trickier. Basically, you want to keep the end dates and use them to calculate the start dates -- for all but the first row:

with toupdate as (
      select t.*,
             lag(enddate) over (partition by employee order by enddate) as prev_enddate,
             row_number() over (partition by employee order by startdate) as seqnum
      from t
     ) t
update toupdate
    set startdate = dateadd(day, 1, prev_enddate)
    where seqnum <> 1 and
          startdate <> dateadd(day, 1, prev_enddate);

您需要 seqnum(或類似的東西),因為在這種情況下,您不能保證最早的開始日期具有最早的結束日期.

You need seqnum (or something similar) because you are not guaranteed that the earliest start date has the earliest end date in this situation.

這篇關于通過在 BI_StartDate 中添加一天避免重疊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 亚洲最新在线视频 | 国产精品99精品久久免费 | 国产区精品 | 国产福利在线 | 福利久久| 7777在线 | 久久精品中文字幕 | 欧美二三区 | 99精品国自产在线观看 | 男女视频在线免费观看 | 91免费看片 | 我要看黄色录像一级片 | 中文字幕亚洲无线 | 亚洲美女一区二区三区 | 午夜视频免费在线观看 | 久久精品二区亚洲w码 | 99精品网| 激情亚洲 | 国产精品美女久久久久久久久久久 | 国产超碰人人爽人人做人人爱 | 日韩国产一区二区三区 | 日韩国产中文字幕 | 亚洲成人一区二区 | 91佛爷在线观看 | 欧美一区二区三区一在线观看 | 亚洲免费影院 | 国产一区二区麻豆 | 麻豆精品一区二区三区在线观看 | 成人精品一区二区三区中文字幕 | 成年人黄色一级毛片 | 久久r精品| 亚洲成人精品一区 | 亚洲一区视频在线 | 日韩在线免费播放 | 欧美日韩第一页 | 日韩成人久久 | 色婷婷综合久久久中字幕精品久久 | 国产精品不卡视频 | 亚洲国产欧美精品 | 中文字幕一区二区三区四区 | 99久久久国产精品 |