In SQL Server 2008 I have a table holding events and the datetime of the event. In a different table I have a list of possible time intervals as shown below. Does anyone have an example on how I can select from my events table only when the event datetime falls inside the intervals in my interval table?
FromDate ToDate
-----------------------------------------------
2012-11-29 06:00:00.000 2012-11-29 07:00:00.000
2012-11-29 07:00:00.000 2012-11-29 08:00:00.000
2012-11-29 09:00:00.000 2012-11-29 10:00:00.000
This is a base query that you can work from. It selects a single interval, and makes sure the
EventDateis between its date range. With this, you can then modify the query to do things like aggregate data in each interval (GROUP BY‘s for example) and so on.