Thursday, March 01, 2012

SQL 2008 script to populate Data dimension table (revised)

Purpose
For a business app (not a datawarehouse) I can use the date dimension table too. But I prefer not to work with a primarey key that is an autoincrement value, as I will be using it in time registration tables, that can benefit from showing a date value and not an autoincrement ID that is nondescriptive. I will be using the data table as a lookup table for entering date on specific dates, i.e. not weekends and for me it sounded easier to create a table for this than to create code for date rules in the business tier. This way a user with sufficient permission has the added flexibility to make a data available e.g. on weekend, that would otherwise be declared as a rule in code.
Finally the DATE type in SQL 2008 is not taking up a lot of space either.
Also I have clarified the naming of my columns, instead of having a column Quarter or Month, it is now called QuarterOfYear and MonthOfYear. This indicates better what the content is and the names don't conflict with sql server functions, thereby avoiding having to type square brackets around the names [Month].
Create the table statement
CREATE TABLE [dbo].[DateDim](

[ActualDate] [date] NOT NULL,
[ActualYear] [int] NOT NULL,
[QuarterOfYear] [int] NOT NULL,
[MonthOfYear] [int] NOT NULL,
[WeekOfYear] [int] NOT NULL,
[DayOfYear] [smallint] NOT NULL,
[DayOfMonth] [smallint] NOT NULL,
[DayOfWeek] [smallint] NOT NULL,
[IsWeekend] [bit] NOT NULL,
[IsHoliday] [bit] NOT NULL,
[CalendarWeek] [smallint] NOT NULL,
[BusinessYearWeek] [smallint] NOT NULL,
[LeapYear] [bit] NOT NULL,
CONSTRAINT [PK_DateDim] PRIMARY KEY CLUSTERED
(
[ActualDate] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Stored procedure to populate the table
CREATE PROCEDURE [dbo].[sp_createTimeDim] AS
--declare variables

DECLARE @DT DATE
DECLARE @YEAR INT
DECLARE @QUARTER INT
DECLARE @MONTH INT
DECLARE @WEEK INT
DECLARE @DayofYear INT
DECLARE @DayofMonth INT
DECLARE @DayofWeek INT
DECLARE @IsWeekend BIT
DECLARE @IsHoliday BIT
DECLARE @CalendarWeek INT
DECLARE @DayName VARCHAR(20)
DECLARE @MonthName VARCHAR(20)
DECLARE @BusinessYearWeek INT
DECLARE @LeapYear BIT

--initialize variables
SELECT @BusinessYearWeek =0
SELECT @CalendarWeek = 1
SELECT @LeapYear =0

--the starting date for the date dimension
SELECT @DT = '2012-01-01'

--start looping, stop at ending date
WHILE (@DT <= '2020-12-31')
BEGIN
--get information about the data
SELECT @IsWeekend =0
SELECT @YEAR = DATEPART (YEAR, @DT)
SELECT @QUARTER = DATEPART (QUARTER, @DT)
SELECT @MONTH = DATEPART (MONTH , @DT)
SELECT @WEEK = DATEPART (WEEK , @DT)
SELECT @DayofYear = DATEPART (DY , @DT)
SELECT @DayofMonth = DATEPART (DAY , @DT)
SELECT @DayofWeek = DATEPART (DW , @DT)

--note if weeknd or not
IF ( @DayofWeek = 1 OR @DayofWeek = 7 )
BEGIN
SELECT @IsWeekend = 1
END

--add 1 every time we start a new week
IF ( @DayofWeek = 1)
BEGIN
SELECT @CalendarWeek = @CalendarWeek +1
END

--add business rule (need to know complete weeks in a year, so a partial week in new year set to 0)
IF ( @DayofWeek != 1 AND @DayofYear = 1)
BEGIN
SELECT @BusinessYearWeek = 0
END

IF ( @DayofWeek = 1)
BEGIN
SELECT @BusinessYearWeek = @BusinessYearWeek +1
END

--add business rule (start counting business weeks with first complete week)
IF (@BusinessYearWeek =53)
BEGIN
SELECT @BusinessYearWeek = 1
END

--check for leap year
IF ((@YEAR % 4 = 0) AND (@YEAR % 100 != 0 OR @YEAR % 400 = 0))
SELECT @LeapYear =1
ELSE SELECT @LeapYear =0

--insert values into Date Dimension table
INSERT dbo.DateDim (ActualDate, [ActualYear], [QuarterOfYear], [MonthOfYear], [WeekofYear], [DayofYear], [DayofMonth], [DayofWeek], IsWeekend, CalendarWeek, BusinessYearWeek, LeapYear, IsHoliday)
VALUES (@DT, @YEAR, @QUARTER, @MONTH, @WEEK, @DayofYear, @DayofMonth, @DayofWeek, @IsWeekend, @CalendarWeek, @BusinessYearWeek, @LeapYear, 0)

--increment the date one day
SELECT @DT = DATEADD(DAY, 1, @DT)
END


GO
The resulting table
ActualDate ActualYear QuarterOfYear MonthOfYear WeekOfYear DayOfYear DayOfMonth DayOfWeek IsWeekend IsHoliday CalendarWeek BusinessYearWeek LeapYear

---------- ----------- ------------- ----------- ----------- --------- ---------- --------- --------- --------- ------------ ---------------- --------
2012-01-01 2012 1 1 1 1 1 1 1 0 2 1 1
2012-01-02 2012 1 1 1 2 2 2 0 0 2 1 1
2012-01-03 2012 1 1 1 3 3 3 0 0 2 1 1
2012-01-04 2012 1 1 1 4 4 4 0 0 2 1 1
2012-01-05 2012 1 1 1 5 5 5 0 0 2 1 1
2012-01-06 2012 1 1 1 6 6 6 0 0 2 1 1
2012-01-07 2012 1 1 1 7 7 7 1 0 2 1 1
2012-01-08 2012 1 1 2 8 8 1 1 0 3 2 1
2012-01-09 2012 1 1 2 9 9 2 0 0 3 2 1
2012-01-10 2012 1 1 2 10 10 3 0 0 3 2 1
2012-01-11 2012 1 1 2 11 11 4 0 0 3 2 1
2012-01-12 2012 1 1 2 12 12 5 0 0 3 2 1

2 comments:

Anonymous said...

louis vuitton outlet, sac longchamp, ralph lauren pas cher, replica watches, louboutin outlet, louboutin shoes, christian louboutin outlet, prada outlet, ugg boots, longchamp pas cher, tiffany and co, louis vuitton, louboutin, air jordan pas cher, tory burch outlet, cheap oakley sunglasses, nike outlet, polo ralph lauren outlet, ray ban sunglasses, polo ralph lauren outlet, louis vuitton outlet, nike air max, replica watches, louis vuitton, air max, longchamp outlet, michael kors, oakley sunglasses, chanel handbags, nike free, nike roshe run, oakley sunglasses, burberry, tiffany jewelry, ray ban sunglasses, kate spade outlet, prada handbags, oakley sunglasses, nike air max, louis vuitton, louboutin pas cher, uggs on sale, ray ban sunglasses, oakley sunglasses, longchamp, jordan shoes, gucci outlet, ugg boots, nike free, longchamp outlet

Anonymous said...

hollister, louis vuitton, canada goose outlet, moncler, pandora jewelry, swarovski, louis vuitton, bottes ugg, coach outlet, moncler, supra shoes, montre pas cher, moncler, ugg,uggs,uggs canada, ugg,ugg australia,ugg italia, canada goose, links of london, pandora jewelry, karen millen, doudoune canada goose, juicy couture outlet, moncler, pandora charms, marc jacobs, swarovski crystal, moncler, ugg pas cher, thomas sabo, louis vuitton, moncler outlet, moncler, canada goose, canada goose uk, canada goose outlet, ugg boots uk, juicy couture outlet, wedding dresses, moncler, canada goose, toms shoes, louis vuitton, replica watches, sac louis vuitton pas cher, pandora charms, canada goose