码迷,mamicode.com
首页 > 其他好文 > 详细

日期维度(周一为每周第一天)

时间:2015-05-07 16:23:15      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Dim_日期:

CREATE TABLE [dbo].[Dim_日期](
    [日期ID] [varchar](8) NOT NULL,
    [] [int] NULL,
    [半年] [varchar](6) NULL,
    [] [varchar](2) NULL,
    [] [varchar](4) NULL,
    [] [varchar](6) NULL,
    [星期] [varchar](6) NULL,
    [是否周末] [varchar](4) NULL,
    [] [int] NULL,
 CONSTRAINT [PK_Dim_日期] PRIMARY KEY CLUSTERED 
(
    [日期ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

生成数据:

SET DATEFIRST 1  --设置周一每周第一天,默认为7

DECLARE @BeginDate DATE;
SELECT @BeginDate = 2000-1-2;
WHILE @BeginDate <= 2016-12-31
  BEGIN
      INSERT INTO [Dim_日期]
      SELECT CONVERT(VARCHAR(8), @BeginDate, 112)  AS [日期ID],
             Year(@BeginDate)                      AS [],
     CASE 
     when DATEPART(month, @BeginDate)<=6 then 上半年
     else 下半年 end as [半年],
             CASE 
               WHEN Datename (qq, @BeginDate) = 1 THEN Q1
               WHEN Datename (qq, @BeginDate) = 2 THEN Q2
               WHEN Datename (qq, @BeginDate) = 3 THEN Q3
               ELSE Q4
             END                                   AS [],
             CASE
               WHEN Month(@BeginDate) = 1 THEN 01月
               WHEN Month(@BeginDate) = 2 THEN 02月
               WHEN Month(@BeginDate) = 3 THEN 03月
               WHEN Month(@BeginDate) = 4 THEN 04月
               WHEN Month(@BeginDate) = 5 THEN 05月
               WHEN Month(@BeginDate) = 6 THEN 06月
               WHEN Month(@BeginDate) = 7 THEN 07月
               WHEN Month(@BeginDate) = 8 THEN 08月
               WHEN Month(@BeginDate) = 9 THEN 09月
               WHEN Month(@BeginDate) = 10 THEN 10月
               WHEN Month(@BeginDate) = 11 THEN 11月
               ELSE 12月
             END                                   AS [],
             CASE
               WHEN Datepart(WEEK, @BeginDate) < 10 THEN 第0 + CONVERT(VARCHAR(2), Datepart(WEEK, @BeginDate)) + 
               ELSE  + CONVERT(VARCHAR(2), Datepart(WEEK, @BeginDate)) + 
             END                                   AS [],
             Datename(WeekDay, @BeginDate)         AS [星期],
             CASE
               WHEN Datepart(dw, @BeginDate) = 6 THEN 周末
               WHEN Datepart(dw, @BeginDate) = 7 THEN 周末
               ELSE 平时
             END                                   AS 是否周末,
             Day(@BeginDate)                       AS []
      SET @BeginDate = Dateadd(DAY, 1, @BeginDate);
  END; 

 

日期维度(周一为每周第一天)

标签:

原文地址:http://www.cnblogs.com/ycdx2001/p/4485069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!