Time intervals

  • 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:00

    Event 2: EventStart 12/31/2090 22:00; EventEnd 1/5/2010 22:00

    Event 3: EventStart 1/2/2010 12:00; EventEnd 1/2/2010 14:00

    Event 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:00

    Event 2: IntervalStart 1/1/2010 00:00; IntervalEnd 1/2/2010 12:00

    Event 2: IntervalStart 1/2/2010 12:00; IntervalEnd 1/2/2010 14:00

    Event 2: IntervalStart 1/2/2010 14:00; IntervalEnd 1/4/2010 12:00

    Event 2: IntervalStart 1/4/2010 12:00; IntervalEnd 1/5/2010 22:00

    Event 3: IntervalStart 1/2/2010 12:00; IntervalEnd 1/2/2010 14:00

    Event 4: IntervalStart 1/4/2010 12:00; IntervalEnd 1/5/2010 22:00

    Event 4: IntervalStart 1/5/2010 22:00; IntervalEnd 2/1/2010 00:00

  • 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.

    ; WITH Dates

    AS

    (

    -- This would be equivalent to 20 years from year 2000

    SELECTDATEADD( d, number, '01-Jan-2000' ) as Date

    FROM(

    SELECTTOP 10000 ROW_NUMBER() OVER( ORDER BY ( SELECT 1 ) ) AS Number

    FROMmaster.sys.columns c1

    cross join master.sys.columns c2

    ) n

    WHEREDATEADD( d, Number, '01-Jan-2000' ) < '31-Dec-2020'

    )

    SELECT*

    FROMInterval I

    CROSS APPLY Dates D

    WHEREI.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 >= I.IntervalStart

    AND D.Date <= I.IntervalEnd

    --Ramesh


  • 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.

    Converting oxygen into carbon dioxide, since 1955.
  • 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.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply