簡體   English   中英

給定日期范圍,查找在該范圍內重疊的事件

[英]Given a date range, find events that overlap within that range

給定輸入日期范圍,例如 11/1/2015 - 11/15/2015,確定在給定日期范圍內發生哪些事件(CalendarEvents)的最有效方法是什么。

event         eventStart     eventEnd
==================================
expo          10/25/2015    11/4/2015       //This should be selected.

concert       11/4/2014      11/5/2015      //This should be selected.

exhibit       11/15/2015     12/1/2015      //this should be selected.

display       10/26/2015    10/29/2015      //this should NOT be selected.

Linq 或 SQL 服務器會很棒。 基本上給定一個日期范圍,查找在該范圍內重疊的事件。

我知道我可以用一些代碼“蠻力”,只是想知道我是否錯過了更優雅的東西?

您可以使用StartA <= EndB AND EndA >= StartA來獲取重疊日期:

DECLARE @startDate  DATE = '20151101',
        @endDate    DATE = '20151115'
SELECT * 
FROM CalendarEvents
WHERE
    eventStart <= @endDate
    AND eventEnd >= @startDate

SQL小提琴

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM