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

SSAS中日期维度表的建立

时间:2014-11-19 18:20:58      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   color   sp   数据   on   div   art   

--创建Dim_Date
CREATE TABLE [dbo].[Dim_Date](
    [Datekey] [varchar](10) NOT NULL,
    [Year] [int] NULL,
    [Quarter] [int] NULL,
    [QuarterCN] [varchar](8) NULL,
    [Mnoth] [int] NULL,
    [MonthCN] [varchar](6) NULL,
    [Ten] [int] NULL,
    [TenCN] [varchar](4) NULL,
    [Week] [int] NULL,
    [WeekDay] [nvarchar](30) NULL,
    [Day] [int] NULL,
    [Date] [varchar](10) NULL,
PRIMARY KEY CLUSTERED 
(
    [Datekey] 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
--清空Dim_Date
TRUNCATE TABLE dbo.Dim_Date;
--循环插入数据
DECLARE @BeginDate DATE;
SET @BeginDate=2009-1-1;
WHILE @BeginDate<GETDATE()
BEGIN
INSERT INTO Dim_Date
    SELECT CONVERT(VARCHAR(10),@BeginDate,112) AS Datekey, --主键
    YEAR(@BeginDate) AS Year,--
    DATEPART(Quarter ,@BeginDate) AS Quarter,
    CASE 
        WHEN DATEPART(Quarter ,@BeginDate)=1 THEN 第一季度 
        WHEN DATEPART(Quarter,@BeginDate)=2 THEN  第二季度
        WHEN DATEPART(Quarter ,@BeginDate)=3 THEN 第三季度 
        WHEN DATEPART(Quarter,@BeginDate)=4 THEN  第四季度
        END AS  QuarterCN,
        MONTH(@BeginDate) AS Mnoth,
    CASE 
        WHEN MONTH(@BeginDate)=1 THEN 一月
        WHEN MONTH(@BeginDate)=2 THEN 二月
        WHEN MONTH(@BeginDate)=3 THEN 三月
        WHEN MONTH(@BeginDate)=4 THEN 四月
        WHEN MONTH(@BeginDate)=5 THEN 五月
        WHEN MONTH(@BeginDate)=6 THEN 六月
        WHEN MONTH(@BeginDate)=7 THEN 七月
        WHEN MONTH(@BeginDate)=8 THEN 八月
        WHEN MONTH(@BeginDate)=9 THEN 九月
        WHEN MONTH(@BeginDate)=10 THEN 十月
        WHEN MONTH(@BeginDate)=11 THEN 十一月
        ELSE 十二月
    END  AS MonthCN,
    CASE 
        WHEN DAY(@BeginDate)<=10 THEN 1
        WHEN DAY(@BeginDate)>20 THEN 3
        ELSE 2
    END AS Ten,--
    CASE 
        WHEN DAY(@BeginDate)<=10 THEN 上旬
        WHEN DAY(@BeginDate)>20 THEN 下旬
        ELSE 中旬
    END AS TenCN,
    DATEPART(week,@BeginDate) AS Week,--
    DATENAME(weekday,@BeginDate) AS WeekDay,--星期
    DAY(@BeginDate) AS Day,
    CONVERT(VARCHAR(10),@BeginDate,120) AS Date
    SET @BeginDate=DATEADD(D,1,@BeginDate);
END

 

SSAS中日期维度表的建立

标签:style   blog   ar   color   sp   数据   on   div   art   

原文地址:http://www.cnblogs.com/xymBlog/p/4108465.html

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