惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
Vercel News
Vercel News
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
H
Help Net Security
罗磊的独立博客
The Cloudflare Blog
J
Java Code Geeks
博客园 - 叶小钗
I
InfoQ
B
Blog
Blog — PlanetScale
Blog — PlanetScale
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
腾讯CDC
月光博客
月光博客
博客园_首页
雷峰网
雷峰网
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
美团技术团队
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美

博客园 - 董晓涛

如何将存储过程执行后的结果集放入临时表 Detect SQL timeout from ASP & Issue RollbackTrans SQL Server 2005 symmetric encrytion sample 如何解决:Error 14274: 无法添加、更新或删除从MSX服务器上发起的作业. Useful Links(to Learn SQL Server) Encrypting Data With the SQL Server Encrypt Function Convert IP To Numberic Generate table structure Generate Insert data script on a table. - 董晓涛 Execute T-SQL asynchronously Don't Use Select * Sql Server数据库被置疑后解决方法 SQL Server 2005中对BLOB的支持(ntext,text and image) SQL Server 2000 Service Pack 4 is released 数据规范化 Microsoft SQL Server 2005 and Vs.net 2005 April 2005 Version XML IN 20 MINUTES! CLR Integrated in SQL Server 2005 XQuery in SQL Server 2005
Generate Time Dim
董晓涛 · 2005-06-29 · via 博客园 - 董晓涛

CREATE TABLE [dbo].[DateDim] (
    [DateKey] [int] IDENTITY (1, 1) NOT NULL ,
    [ActualDate] [datetime] NOT NULL ,
    [Year] [int] NOT NULL ,
    [Quarter] [int] NOT NULL ,
    [Month] [int] NOT NULL ,
    [Week] [int] NOT NULL ,
    [DayofYear] [int] NOT NULL ,
    [DayofMonth] [int] NOT NULL ,
    [DayofWeek] [int] NOT NULL ,
    [IsWeekend] [bit] NOT NULL ,
    [IsHoliday] [bit] NOT NULL ,
    [Comments] [varchar] (20) COLLATE Latin1_General_CI_AI NULL ,
    [CalendarWeek] [int] NOT NULL ,
    [BusinessYearWeek] [int] NOT NULL ,
    [LeapYear] [tinyint] NOT NULL
) ON [PRIMARY]
GO

CREATE PROCEDURE sp_createTimeDim  AS

--delete contents of Date Dimension Table
TRUNCATE TABLE DateDim

--declare variables
DECLARE @DT DATETIME
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  = '1/1/1998'

--start looping, stop at ending date
WHILE (@DT <= '1/31/2005')
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 DateDim  (ActualDate, Year, Quarter, Month, Week, DayofYear, DayofMonth, DayofWeek, IsWeekend, CalendarWeek, BusinessYearWeek, LeapYear)
 VALUES (@DT, @YEAR, @QUARTER, @MONTH, @WEEK, @DayofYear, @DayofMonth, @DayofWeek, @IsWeekend, @CalendarWeek, @BusinessYearWeek, @LeapYear)

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

END
GO