Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Time intervals


Time intervals

Author
Message
gary-837136
gary-837136
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 41
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
Ramesh Saive
Ramesh Saive
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2722 Visits: 2643
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
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' ) < '31-Dec-2020'
)
SELECT *
FROM Interval I
CROSS APPLY Dates D
WHERE 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 >= I.IntervalStart
AND D.Date <= I.IntervalEnd




--Ramesh


Steve Cullen
Steve Cullen
SSChasing Mays
SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)SSChasing Mays (639 reputation)

Group: General Forum Members
Points: 639 Visits: 1226
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.


Shaira
Shaira
SSC-Enthusiastic
SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)SSC-Enthusiastic (104 reputation)

Group: General Forum Members
Points: 104 Visits: 276
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search