1、首先要构建一个时间维度表,下面给出通用的构建时间维度的存储过程:
USE [BI_DW] GO /****** Object: StoredProcedure [dbo].[proc_Dim_date] Script Date: 05/20/2016 11:35:58 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[proc_Dim_date] as begin IF OBJECT_ID(‘dbo.Dim_Date‘) IS NOT NULL DROP TABLE dbo.[Dim_Date] CREATE TABLE [dbo].[Dim_Date]( [DateKey] [int] NULL, [Date] [datetime] NULL, [Year] [float] NULL, [Month] [float] NULL, [Month EN] [nvarchar](50) NULL, [Month Short EN] [nvarchar](50) NULL, [Month CN] [nvarchar](50) NULL, [Day] [float] NULL, [Quarter] [float] NULL, [Quarter EN] [nvarchar](50) NULL, [Quarter CN] [nvarchar](50) NULL, [Weekday] [float] NULL, [Weekday CN] [nvarchar](50) NULL, [Weekday Short EN] [nvarchar](50) NULL, [Week of Year] [float] NULL, [Day of Year] [float] NULL, [SemiYearly] [nvarchar](50) NULL, [Period of Ten Days] [nvarchar](10) NULL, [Period of Index] [nvarchar](2) NULL, [Weekend] [nvarchar](5) NULL ) ON [PRIMARY] SET DATEFIRST 7 --设周日为每周的第一天 --向日期表插入数据 DECLARE @b1 DATETIME set @b1=‘2010-01-01‘ --设置起始日期 WHILE @b1<dateadd(day,2,GETDATE()) --设置截止日期 BEGIN INSERT INTO dbo.[Dim_Date] ( [DateKey], [Date], [Year], [Month], [Month EN], [Month Short EN], [Month CN], [Day], [Quarter], [Quarter EN], [Quarter CN], [Weekday], [Weekday CN], [Weekday Short EN], [Week of Year], [Day of Year], [SemiYearly], [Period of Ten Days], [Period of Index] , [Weekend] ) VALUES( CONVERT(NVARCHAR(10),@b1,112), --DateKey 1 @b1, --Date 2 DATEPART(year, @b1), --Year 3 DATEPART(month, @b1), --Month 4 CASE --Month EN 5 when (DATEPART(month, @b1))=‘1‘ then ‘January‘ when (DATEPART(month, @b1))=‘2‘ then ‘February‘ when (DATEPART(month, @b1))=‘3‘ then ‘March‘ when (DATEPART(month, @b1))=‘4‘ then ‘April‘ when (DATEPART(month, @b1))=‘5‘ then ‘May‘ when (DATEPART(month, @b1))=‘6‘ then ‘June‘ when (DATEPART(month, @b1))=‘7‘ then ‘July‘ when (DATEPART(month, @b1))=‘8‘ then ‘August‘ when (DATEPART(month, @b1))=‘9‘ then ‘September‘ when (DATEPART(month, @b1))=‘10‘ then ‘October‘ when (DATEPART(month, @b1))=‘11‘ then ‘November‘ else ‘December‘ END, CASE --Month Short En 6 when (DATEPART(month, @b1))=‘1‘ then ‘Jan‘ when (DATEPART(month, @b1))=‘2‘ then ‘Feb‘ when (DATEPART(month, @b1))=‘3‘ then ‘Mar‘ when (DATEPART(month, @b1))=‘4‘ then ‘Apr‘ when (DATEPART(month, @b1))=‘5‘ then ‘May‘ when (DATEPART(month, @b1))=‘6‘ then ‘Jun‘ when (DATEPART(month, @b1))=‘7‘ then ‘Jul‘ when (DATEPART(month, @b1))=‘8‘ then ‘Aug‘ when (DATEPART(month, @b1))=‘9‘ then ‘Sep‘ when (DATEPART(month, @b1))=‘10‘ then ‘Oct‘ when (DATEPART(month, @b1))=‘11‘ then ‘Nov‘ else ‘Dec‘ END, CASE --Month CN 7 when (DATEPART(month, @b1))=‘1‘ then N‘一月‘ when (DATEPART(month, @b1))=‘2‘ then N‘二月‘ when (DATEPART(month, @b1))=‘3‘ then N‘三月‘ when (DATEPART(month, @b1))=‘4‘ then N‘四月‘ when (DATEPART(month, @b1))=‘5‘ then N‘五月‘ when (DATEPART(month, @b1))=‘6‘ then N‘六月‘ when (DATEPART(month, @b1))=‘7‘ then N‘七月‘ when (DATEPART(month, @b1))=‘8‘ then N‘八月‘ when (DATEPART(month, @b1))=‘9‘ then N‘九月‘ when (DATEPART(month, @b1))=‘10‘ then N‘十月‘ when (DATEPART(month, @b1))=‘11‘ then N‘十一月‘ else N‘十二月‘ END, DATEPART(day, @b1),--day 8 DATEName (qq, @b1),--quarter 9 CASE --quarter en 10 when DATEName (qq, @b1)=‘1‘ then ‘Q1‘ when DATEName (qq, @b1)=‘2‘ then ‘Q2‘ when DATEName (qq, @b1)=‘3‘ then ‘Q3‘ else ‘Q4‘ END, CASE --quarter cn 11 when DATEName (qq, @b1)=‘1‘ then N‘一季度‘ when DATEName (qq, @b1)=‘2‘ then N‘二季度‘ when DATEName (qq, @b1)=‘3‘ then N‘三季度‘ else N‘四季度‘ END, DATEPART(dw, @b1),--Weekday 12 CASE --Weekday CN 13 when DATEPART(dw, @b1)=1 then N‘星期日‘ when DATEPART(dw, @b1)=2 then N‘星期一‘ when DATEPART(dw, @b1)=3 then N‘星期二‘ when DATEPART(dw, @b1)=4 then N‘星期三‘ when DATEPART(dw, @b1)=5 then N‘星期四‘ when DATEPART(dw, @b1)=6 then N‘星期五‘ else N‘星期六‘ END, CASE --Weekday Short EN 14 --注意,周日是第一天. when DATEPART(dw, @b1)=‘1‘ then ‘Sun‘ when DATEPART(dw, @b1)=‘2‘ then ‘Mon‘ when DATEPART(dw, @b1)=‘3‘ then ‘Tue‘ when DATEPART(dw, @b1)=‘4‘ then ‘Wed‘ when DATEPART(dw, @b1)=‘5‘ then ‘Thu‘ when DATEPART(dw, @b1)=‘6‘ then ‘Fri‘ else ‘Sat‘ END, DATEName (wk, @b1),--week of year 15 DATEName (dy, @b1),--day of year 16 CASE --SemiYearly 17 when DATEPART(month, @b1)<=6 then N‘上半年‘ else N‘下半年‘ END, CASE --Period of Ten Days 18 when DATEName (dd, @b1)<=10 then N‘上旬‘ when DATEName (dd, @b1)>20 then N‘下旬‘ else N‘中旬‘ END, CASE --Period of Ten Days 19 when DATEName (dd, @b1)<=10 then N‘1‘ when DATEName (dd, @b1)>20 then N‘3‘ else N‘2‘ END, CASE --Is it Weekend? 20 when DATEPART(dw, @b1)=‘1‘ then ‘周末‘ when DATEPART(dw, @b1)=‘7‘ then ‘周末‘ else ‘平时‘ END ) --日期加1天 set @b1=DATEADD(day, 1, @b1) END end
2、在数据库中构建这样的维度表之后,在SSAS出新建维度,这里就不再多说了。
重点介绍如何构建一个标准的时间维度,首先,打开维度设计选项卡,添加下面两套成员用来构建标准时间维度,按照图中的样子:
3、设置属性:
3.1 将Dim Date的属性“TYPE”设置为:Time
3.2 将带有“层级”的成员属性设置为:以“Day-层级”为例:Type =Days; KeyColumms =‘内容见“键列”图‘,这里选择了组合键(确保在层级中“Day”是唯一的);所以需要设置NameColumns,将NameColumns=‘Day‘用来显示成员而已。
3.3 带有层级的其他成员都按照3.2的设置,具体有不同:Year-层级的Type=‘Years‘ ,Month-层级的Type=‘Months‘ ,Quater-层级的Type=‘Quaters‘;当然还要选择NameColumn的相应字段用来显示。KeyColumns的组合字段的选择,只要符合保证唯一即可,例如Month的KeyColumns:
3.4 按照以上步骤,设置好之后,还是不行的,还需要将属性关系设置合理才行,按照下图的属性关系设置即可;设置成员的属性的时候,最好将成员的order by属性设置为“Key”,这样浏览的数据就会按照key排序了;除了带有层级的成员属性外,其他属性可以不需要额外设置即可;
3.5 这样处理维度,浏览查看效果:
时间: 2024-10-26 19:02:41