﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / Time intervals / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 10:23:49 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Time intervals</title><link>http://www.sqlservercentral.com/Forums/Topic862332-392-1.aspx</link><description>I created my time calendar using the wizard in SSAS (Generate a Time Table in the Data Source).  Very simple to use and creates a very detailed table, including fiscal dates, etc.</description><pubDate>Wed, 10 Feb 2010 09:25:35 GMT</pubDate><dc:creator>Shaira</dc:creator></item><item><title>RE: Time intervals</title><link>http://www.sqlservercentral.com/Forums/Topic862332-392-1.aspx</link><description>We find it handy to keep a static calendar table around with dates and attributes such as IsHolliday and DayOfWeek, Month, Quarter, FiscalYear etc.Ours starts at 1/1/1900 and goes to 12/31/2099.</description><pubDate>Tue, 09 Feb 2010 09:34:04 GMT</pubDate><dc:creator>Steve Cullen</dc:creator></item><item><title>RE: Time intervals</title><link>http://www.sqlservercentral.com/Forums/Topic862332-392-1.aspx</link><description>You either have to use a calendar table or generate a calendar table on the fly and cross apply this table the event table as per the interval type.For example, this following should work for yearly interval though I've not tested it myself.[code]; WITH DatesAS(	-- This would be equivalent to 20 years from year 2000	SELECT	DATEADD( d, number, '01-Jan-2000' ) as Date	FROM	(				SELECT	TOP 10000 ROW_NUMBER() OVER( ORDER BY ( SELECT 1 ) ) AS Number				FROM	master.sys.columns c1						cross join master.sys.columns c2			) n	WHERE	DATEADD( d, Number, '01-Jan-2000' ) &amp;lt; '31-Dec-2020')SELECT	*FROM	Interval I		CROSS APPLY Dates DWHERE	I.IntervalType = 'Year'		AND 		(			DATEADD( DAY, DATEDIFF( DAY, 0, I.IntervalStart ), 0 ) = D.Date 			OR DATEADD( DAY, DATEDIFF( DAY, 0, I.IntervalEnd ), 0 ) = D.Date			OR 			(				DAY( I.IntervalStart ) = DAY( D.Date )				AND MONTH( I.IntervalStart ) = MONTH( D.Date )			)		)		AND D.Date &amp;gt;= I.IntervalStart		AND D.Date &amp;lt;= I.IntervalEnd[/code]</description><pubDate>Tue, 09 Feb 2010 06:03:09 GMT</pubDate><dc:creator>Ramesh Saive</dc:creator></item><item><title>Time intervals</title><link>http://www.sqlservercentral.com/Forums/Topic862332-392-1.aspx</link><description>I'm hoping there's a guru here that can help me get this thing optimized. What I have now is quite ugly, using cursors and taking forever.This is used in pre-compiling some data for reports. A pre-compiled data set is called a RepSet. Intervals can be Year, Quarter, Month, Week-Su, Week-Sa, Week-Mo or Daily.CREATE TABLE [dbo].[Interval](	[RepSetID] [smallint] NOT NULL,	[IntervalStart] [datetime] NOT NULL,	[IntervalEnd] [datetime] NOT NULL,	[IntervalType] [tinyint] NULL, CONSTRAINT [PK_Interval] PRIMARY KEY CLUSTERED (	[RepSetID] ASC,	[IntervalStart] ASC,	[IntervalEnd] ASC)Then there are events that look something like this:CREATE TABLE [dbo].[RepSetEvent](	[RepSetID] [smallint] NOT NULL,	[UnitID] [smallint] NOT NULL,	[EventID] [int] NOT NULL,	[EventSequence] [smallint] NULL,	[EventStart] [smalldatetime] NULL,	[EventEnd] [smalldatetime] NULL,	[EventType] [char](2) NULL CONSTRAINT [PK_RepSetEvent] PRIMARY KEY CLUSTERED (	[RepSetID] ASC,	[UnitID] ASC,	[EventID] ASC)Using the above two tables, I need to create a third, which has events by interval:CREATE TABLE [dbo].[EventIntervals](	[repsetID] [smallint] NOT NULL,	[eventID] [int] NOT NULL,	[intervalStart] [smalldatetime] NOT NULL,	[unitid] [smallint] NOT NULL,	[intervalEnd] [smalldatetime] NULL,	[EventType] [char](2) NULL CONSTRAINT [PK_EventIntervals] PRIMARY KEY CLUSTERED (	[repsetID] ASC,	[eventID] ASC,	[intervalStart] ASC)The EventIntervals table will have at least one record per event. If an event crosses the reporting interval boundary, the event is then split into a separate interval for each reporting interval. If an event has overlapping events, then overlapping time spans become seperate event intervals.Example:Monthly reporting period. IntervalStart=1/1/2010 00:00, IntervalEnd=2/1/2010 00:00.Event 1: EventStart 1/1/2010 13:00; EventEnd 1/1/2010 14:00Event 2: EventStart 12/31/2090 22:00; EventEnd 1/5/2010 22:00Event 3: EventStart 1/2/2010 12:00; EventEnd 1/2/2010 14:00Event 4: EventStart 1/4/2010 12:00; EventEnd NULL (event in progress)Should result in the following event intervals:Event 1: IntervalStart 1/1/2010 13:00; IntervalEnd 1/1/2010 14:00Event 2: IntervalStart 1/1/2010 00:00; IntervalEnd 1/2/2010 12:00Event 2: IntervalStart 1/2/2010 12:00; IntervalEnd 1/2/2010 14:00Event 2: IntervalStart 1/2/2010 14:00; IntervalEnd 1/4/2010 12:00Event 2: IntervalStart 1/4/2010 12:00; IntervalEnd 1/5/2010 22:00Event 3: IntervalStart 1/2/2010 12:00; IntervalEnd 1/2/2010 14:00Event 4: IntervalStart 1/4/2010 12:00; IntervalEnd 1/5/2010 22:00Event 4: IntervalStart 1/5/2010 22:00; IntervalEnd 2/1/2010 00:00</description><pubDate>Tue, 09 Feb 2010 04:41:45 GMT</pubDate><dc:creator>gary-837136</dc:creator></item></channel></rss>