Cube中时间维度

系统 1536 0

创建DW中时间维度表DimDate

      
        CREATE
      
      
        TABLE
      
      
        [
      
      
        dbo
      
      
        ]
      
      .
      
        [
      
      
        DimDate
      
      
        ]
      
      
        (  


      
      
        [
      
      
        TimeKey
      
      
        ]
      
      
        int
      
      
        NOT
      
      
        NULL
      
      
        ,  

    
      
      
        [
      
      
        TheDate
      
      
        ]
      
      
        datetime
      
      
        NULL
      
      , 
      
        --
      
      
        DateTime格式的日期  
      
      
        [
      
      
        TheDateName
      
      
        ]
      
      
        nvarchar
      
      (
      
        10
      
      ) 
      
        NULL
      
      , 
      
        --
      
      
        日期名称  
      
      
        [
      
      
        TheYear
      
      
        ]
      
      
        smallint
      
      
        NULL
      
      ,
      
        --
      
      
        年份  
      
      
        [
      
      
        TheYearName
      
      
        ]
      
      
        nvarchar
      
      (
      
        10
      
      )
      
        NULL
      
      ,
      
        --
      
      
        年份名称  
      
      
        [
      
      
        TheMonth
      
      
        ]
      
      
        smallint
      
      
        NULL
      
      ,
      
        --
      
      
        月份  
      
      
        [
      
      
        TheMonthName
      
      
        ]
      
      
        nvarchar
      
      (
      
        10
      
      )
      
        NULL
      
      ,
      
        --
      
      
        月份名称  
      
      
        [
      
      
        TheDay
      
      
        ]
      
      
        smallint
      
      
        NULL
      
      ,
      
        --
      
      
      
        [
      
      
        TheDayName
      
      
        ]
      
      
        nvarchar
      
      (
      
        10
      
      )
      
        NULL
      
      ,
      
        --
      
      
        日的名称  
      
      
        [
      
      
        TheQuarter
      
      
        ]
      
      
        smallint
      
      
        NULL
      
      ,
      
        --
      
      
        季度  
      
      
        [
      
      
        TheQuarterName
      
      
        ]
      
      
        nvarchar
      
      (
      
        10
      
      ) 
      
        NULL
      
      ,
      
        --
      
      
        季度名称  
      
      
        [
      
      
        TheWeek
      
      
        ]
      
      
        smallint
      
      
        NULL
      
      ,
      
        --
      
      
        星期  
      
      
        [
      
      
        TheWeekName
      
      
        ]
      
      
        nvarchar
      
      (
      
        10
      
      )
      
        NULL
      
      ,
      
        --
      
      
        星期名称    
      
      
        [
      
      
        Vacation_Mark
      
      
        ]
      
      
        smallint
      
      
        NULL
      
      
        --
      
      
        节假日标志  
      
      

)
      
        ON
      
      
        [
      
      
        PRIMARY
      
      
        ]
      
      



        插入数据
      

DECLARE @TheDate datetime , @TheDateName nvarchar ( 10 ), @TheYear smallint , @TheYearName nvarchar ( 10 ), @TheMonth smallint , @TheMonthName nvarchar ( 10 ), @TheDay smallint , @TheDayName nvarchar ( 10 ), @TheQuarter smallint , @TheQuarterName nvarchar ( 10 ), @TheWeek smallint , @TheWeekName nvarchar ( 10 ), @Vacation_Mark smallint , @timeKey int , @dDate DATETIME , -- 存储起始日期和结束日期 @adddays smallint -- 存储日期增量 set @adddays = 1 -- 日期增量 set @dDate = ' 1/1/2005 ' -- 当前日期 WHILE @dDate <= ' 12/31/2020 ' -- 结束日期 BEGIN set @TheDate = @dDate set @timeKey = cast (( left ( convert ( nvarchar , @TheDate , 23 ), 4 ) + substring ( convert ( nvarchar , @TheDate , 23 ), 6 , 2 ) + substring ( convert ( nvarchar , @TheDate , 23 ), 9 , 2 )) as int ) set @TheDateName = REPLACE ( CONVERT ( nvarchar ( 20 ), @dDate , 111 ), ' / ' , ' - ' ) set @TheYear = DATENAME (yy, @dDate ) set @TheYearName = CAST ( @TheYear as nvarchar ) + ' ' set @TheMonth = DATENAME (mm, @dDate ) set @TheMonthName = CAST ( @TheMonth as nvarchar ) + ' ' set @TheDay = DATENAME (dd, @dDate ) set @TheDayName = CAST ( @TheDay as nvarchar ) + ' ' set @TheQuarter = DATENAME (Quarter, @dDate ) set @TheQuarterName = ' ' + CAST ( DATENAME (Quarter, @dDate ) as varchar ( 1 )) + ' 季度 ' set @TheWeek = DATEPART (dw, @dDate ) set @TheWeekName = DATENAME (dw, @dDate ) set @Vacation_Mark = CASE WHEN ( @TheWeek = 1 OR @TheWeek = 7 ) THEN 1 ELSE 0 END insert INTO DimDate(TimeKey,TheDate,TheDateName,TheYear,TheYearName,TheMonth,TheMonthName,TheDay, TheDayName,TheQuarter,TheQuarterName,TheWeek,TheWeekName,Vacation_Mark) VALUES ( @timeKey , @TheDate , @TheDateName , @TheYear , @TheYearName , @TheMonth , @TheMonthName , @TheDay , @TheDayName , @TheQuarter , @TheQuarterName , @TheWeek , @TheWeekName , @Vacation_Mark ) set @dDate = @dDate + @adddays END GO

Cube中时间维度


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论