简体   繁体   English

给定日期范围,查找在该范围内重叠的事件

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

Given a input date range, such as 11/1/2015 - 11/15/2015, what is the most efficient way to determine which events(CalendarEvents) are going on during that given date 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 or SQL server would be awesome. Linq 或 SQL 服务器会很棒。 Basically given a date range, find events that overlap within that range.基本上给定一个日期范围,查找在该范围内重叠的事件。

I know I could "brute force" with a bit of code, just wondering if I'm missing something more elegant?我知道我可以用一些代码“蛮力”,只是想知道我是否错过了更优雅的东西?

You can use StartA <= EndB AND EndA >= StartA to get the overlapping dates:您可以使用StartA <= EndB AND EndA >= StartA来获取重叠日期:

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

SQL Fiddle SQL小提琴

Reference 参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM