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

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - 董晓涛

如何将存储过程执行后的结果集放入临时表 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