|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 8:41 AM
Points: 6,
Visits: 9
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Friday, February 22, 2013 12:03 AM
Points: 2,555,
Visits: 2,587
|
|
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
|
|
|
|
|
Mr 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.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 10:38 AM
Points: 52,
Visits: 237
|
|
| 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.
|
|
|
|