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

T-SQL - 獲取最近的日期和最近的未來日期

T-SQL - Getting most recent date and most recent future date(T-SQL - 獲取最近的日期和最近的未來日期)
本文介紹了T-SQL - 獲取最近的日期和最近的未來日期的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

假設下面的記錄表

ID    Name       AppointmentDate
--    --------   ---------------
1     Bob         1/1/2010
1     Bob         5/1/2010
2     Henry       5/1/2010
2     Henry       8/1/2011
3     John        8/1/2011
3     John       12/1/2011

我想按人檢索最近的約會日期.所以我需要一個查詢來提供以下結果集.

I want to retrieve the most recent appointment date by person. So I need a query that will give the following result set.

1   Bob    5/1/2010 (5/1/2010 is most recent)
2   Henry  8/1/2011 (8/1/2011 is most recent)
3   John   8/1/2011 (has 2 future dates but 8/1/2011 is most recent)

謝謝!

推薦答案

假設您說最近"的意思是最近",例如存儲的日期是距當前日期最少的天數,而我們不這樣做"不關心它是在當前日期之前還是之后",那么應該這樣做(可能需要進行瑣碎的調試):

Assuming that where you say "most recent" you mean "closest", as in "stored date is the fewest days away from the current date and we don't care if it's before or after the current date", then this should do it (trivial debugging might be required):

SELECT ID, Name, AppointmentDate
 from (select
           ID
          ,Name
          ,AppointmentDate
          ,row_number() over (partition by ID order by abs(datediff(dd, AppointmentDate, getdate()))) Ranking
         from MyTable) xx
 where Ranking = 1

這使用 SQL 2005 及更高版本的 row_number() 函數.子查詢根據規范對數據進行排序",主查詢選擇最合適的.

This usese the row_number() function from SQL 2005 and up. The subquery "orders" the data as per the specifications, and the main query picks the best fit.

還要注意:

  • 搜索基于當前日期
  • 我們只計算天數差異,忽略時間(小時、分鐘等)
  • 如果兩天是等距的(比如之前 2 天和之后 2 天),我們隨機選擇一個

所有這些都可以根據您的最終要求進行調整.

All of which could be adjusted based on your final requirements.

這篇關于T-SQL - 獲取最近的日期和最近的未來日期的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 九九热最新视频 | 久久久久国产 | 羞羞视频在线观看免费观看 | 国产福利视频网站 | 国产剧情久久 | 一区二区三区四区在线视频 | 一级毛片在线播放 | 国产在线小视频 | 日韩在线 | 蜜桃黄网 | 黑人久久久 | 国产成人精品免费视频大全最热 | 久久午夜视频 | 成人欧美一区二区三区黑人孕妇 | 亚洲欧美一区在线 | 日韩免费在线观看视频 | 一区二区三区国产精品 | 国产片侵犯亲女视频播放 | 精品视频一区二区三区四区 | 日韩精品999 | 男女午夜免费视频 | 男女啪啪网址 | 国产a区 | www成年人视频 | 日日夜夜天天 | 亚洲va国产日韩欧美精品色婷婷 | 国产99久久精品一区二区永久免费 | 国产资源网 | 国产精品一区在线 | 野狼在线社区2017入口 | 久久精品成人 | 亚洲一区二区在线视频 | 成人三区| 亚洲精品免费视频 | 日本国产一区二区 | 国产片侵犯亲女视频播放 | 91麻豆精品国产91久久久更新资源速度超快 | 国产日韩欧美在线 | 久久精品视频网站 | 亚洲高清在线观看 | 久久69精品久久久久久久电影好 |