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

使用一組可能重疊的排除項計算日期范圍內(nèi)的天

Count days in date range with set of exclusions which may overlap(使用一組可能重疊的排除項計算日期范圍內(nèi)的天數(shù))
本文介紹了使用一組可能重疊的排除項計算日期范圍內(nèi)的天數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

給定以下示例查詢,考慮到這些范圍可能具有重疊的日期,在給定一組要排除的范圍的情況下,計算日期范圍內(nèi)總天數(shù)的可靠且高效的方法是什么?

Given the following example query, what is a sound and performant approach to counting the total days in a date range when also given a set of ranges to exclude, given that those ranges may have dates which overlap?

更簡單地說,我有一個表格,其中包含一組關(guān)閉計費(fèi)的日期范圍,我從一個日期范圍(比如 Jan1 - Jan31)開始,我需要確定該范圍內(nèi)發(fā)生了多少可計費(fèi)天.只是日期的日期差異減去禁用日期的日期差異的總和.但是,禁用日期范圍有可能重疊,即禁用一條記錄中的 Jan5-Jan8 和另一條記錄中的 Jan7-Jan10 - 因此簡單的總和會使 Jan7 重復(fù)計算.排除這些重疊并獲得準(zhǔn)確計數(shù)的最佳方法是什么?

More simply, I have a table with a set of date ranges where the billing is turned off, I start with a date range (say Jan1 - Jan31) and I need to determine how many billable days occured in that range. Simply a datediff of the days minus a sum of the datediff on the disabled days. However, there is a chance that the disabled date ranges overlap, ie disabled Jan5-Jan8 in one record and Jan7-Jan10 in another record - thus a simple sum would double count Jan7. What is the best way to exclude these overlaps and get an accurage count.

Declare @disableranges table (disableFrom datetime, disableTo datetime)
insert into @disableranges
select '01/05/2013', '01/08/2013' union
select '01/07/2013', '01/10/2013' union
select '01/15/2013', '01/20/2013'

declare @fromDate datetime = '01/01/2013'
declare @toDate datetime = '01/31/2013'

declare @totalDays int = DATEDIFF(day,@fromDate,@toDate)
declare @disabledDays int = (0 /*not sure best way to calc this*/)

select @totalDays - @disabledDays

推薦答案

您可以使用 遞歸 CTE 在 @dateFrom@dateTo 之間生成日期.然后將日期與范圍進(jìn)行比較,并找到任何范圍內(nèi)的所有日期.最后,計算結(jié)果中的行數(shù)以獲取禁用日期的計數(shù)(演示):

You can use a recursive CTE to generate dates between @dateFrom and @dateTo. Then compare the dates with the ranges, and find all dates that are in any range. Finally, count the number of rows in the result to get the count of disabled dates (DEMO):

-- recursive CTE to generate dates
;with dates as (
  select @fromDate as date
  union all
  select dateadd(day, 1, date)
  from dates
  where date < @toDate
)

-- join with disable ranges to find dates in any range
, disabledDates as (
  select date from dates D
  left join @disableranges R
    on D.date >= R.disableFrom and d.Date < R.disableTo
  group by date
  having count(R.disablefrom) >= 1
)

-- count up the total disabled dates
select @disabledDays=count(*) from disabledDates;

這篇關(guān)于使用一組可能重疊的排除項計算日期范圍內(nèi)的天數(shù)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標(biāo)記轉(zhuǎn)換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創(chuàng)建視圖?)
Create calculated value based on calculated value inside previous row(根據(jù)前一行內(nèi)的計算值創(chuàng)建計算值)
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屋-程序員軟件開發(fā)技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉(zhuǎn)換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 欧美在线观看免费观看视频 | 91视频在线观看 | 91精品国产综合久久久久久首页 | 国产超碰人人爽人人做人人爱 | www.99热| 国产精久久久 | 91视频国产区 | 在线一区视频 | 欧美日韩在线看 | 成人性视频免费网站 | 女女百合av大片一区二区三区九县 | 国产这里只有精品 | 男人天堂99| 亚洲欧美综合精品另类天天更新 | jizz中国日本| 精品国产免费一区二区三区演员表 | 四虎影院在线观看免费视频 | 二区在线视频 | 成人精品啪啪欧美成 | 青青草一区二区三区 | 这里精品| 国产区精品 | a在线免费观看 | 国产一区二区在线观看视频 | 在线观看中文字幕视频 | 97精品超碰一区二区三区 | 亚洲国产精品va在线看黑人 | 久久久精品网 | 久久久久久久久久久久久久国产 | 日本一区二区高清不卡 | 欧美日韩一区二区三区四区五区 | 天天综合日日夜夜 | 日韩免费一级 | 中文字幕在线视频免费视频 | 久久一区二区三区免费 | www,黄色,com | 国产欧美在线 | 日韩精品久久久久 | 久久综合久 | 国产专区免费 | 欧洲毛片 |