Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Time intervals Expand / Collapse
Author
Message
Posted Tuesday, February 9, 2010 4:41 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 9, 2014 10:16 AM
Points: 6, Visits: 21
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


Post #862332
Posted Tuesday, February 9, 2010 6:03 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, October 21, 2013 12:32 AM
Points: 2,551, Visits: 2,593
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

Post #862365
Posted Tuesday, February 9, 2010 9:34 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Wednesday, September 19, 2012 8:39 AM
Points: 595, Visits: 1,226
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.

Post #862543
Posted Wednesday, February 10, 2010 9:25 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, August 29, 2014 7:47 AM
Points: 65, Visits: 259
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.
Post #863380
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse