ssas 时间维度表 sql 代码

该代码转自 天善智能社区 文章 文章地址:https://ask.hellobi.com/blog/biwork/572

---------------------------------------------------------------------
-- BIWORK DimDate and vDimDate Demo
----------------------------------------------------------------------
USE BIWORK_SSIS
GO 
SET NOCOUNT ON

IF OBJECT_ID(‘DimDate‘,‘U‘) IS NOT NULL
DROP TABLE DimDate
GO

CREATE TABLE DimDate
(
    DateKey INT PRIMARY KEY,
    FullDate DATE NOT NULL,
    [DateName] NVARCHAR(20),
    DayNumberOfWeek TINYINT NOT NULL,
    DayNameOfWeek NVARCHAR(10) NOT NULL,
    DayNumberOfMonth TINYINT NOT NULL,
    DayNumberOfYear SMALLINT NOT NULL,
    IsWeekend BIT NOT NULL,
    IsLeapYear BIT NOT NULL,
    WeekNumberOfYear TINYINT NOT NULL,
    EnglishMonthName NVARCHAR(10) NOT NULL,
    MonthNumberOfYear TINYINT NOT NULL,
    CalendarQuarter TINYINT NOT NULL,
    CalendarSemester TINYINT NOT NULL,
    CalendarYear SMALLINT NOT NULL,
    FiscalQuarter TINYINT NOT NULL,
    FiscalSemester TINYINT NOT NULL,
    FiscalYear SMALLINT NOT NULL
)

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME

SELECT @StartDate = ‘2005-01-01‘,
       @EndDate = ‘2013-12-31‘

WHILE (@StartDate <= @EndDate)
BEGIN
    INSERT INTO DimDate
    (
        DateKey,
        FullDate,
        [DateName],
        DayNumberOfWeek,
        DayNameOfWeek,
        DayNumberOfMonth,
        DayNumberOfYear,
        IsWeekend,
        IsLeapYear,
        WeekNumberOfYear,
        EnglishMonthName,
        MonthNumberOfYear,
        CalendarQuarter,
        CalendarSemester,
        CalendarYear,
        FiscalQuarter,
        FiscalSemester,
        FiscalYear
    )
    SELECT CAST(CONVERT(VARCHAR(8),@StartDate,112) AS INT) AS DateKey,
           CONVERT(VARCHAR(10), @StartDate,20) AS FullDate,
           CONVERT(VARCHAR(20), @StartDate,106) AS [DateName],
           DATEPART(DW,@StartDate) AS DayNumberOfWeek,
           DATENAME(DW,@StartDate) AS DayNameOfWeek,
           DATENAME(DD,@StartDate) AS [DayOfMonth],
           DATENAME(DY,@StartDate) AS [DayOfYear],
           CASE WHEN DATEPART(DW,@StartDate) IN (1,7) 
                    THEN 1
                ELSE 0
           END AS IsWeekend,
           CASE WHEN ((YEAR(@StartDate) % 4 = 0) AND (YEAR(@StartDate) % 100 != 0 OR YEAR(@StartDate) % 400 = 0))
                    THEN 1
                ELSE 0
           END AS IsLeapYear,
           DATEPART(WW,@StartDate) AS WeekNumberOfYear,
           DATENAME(MM,@StartDate) AS EnglishMonthName,
           DATEPART(MM,@StartDate) AS MonthNumberOfYear,
           DATEPART(QQ,@StartDate) AS CalendarQuarter,
           CASE WHEN DATEPART(MM,@StartDate) BETWEEN 1 AND 6
                    THEN 1
                ELSE 2
           END AS CalendarSemester,
           DATEPART(YY,@StartDate) AS CalendarYear,
           CASE WHEN DATEPART(MM,@StartDate) BETWEEN 1 AND 6
                     THEN DATEPART(QQ,@StartDate) + 2
                ELSE DATEPART(QQ,@StartDate) - 2
           END AS FiscalQuarter,
           CASE WHEN DATEPART(MM,@StartDate) BETWEEN 1 AND 6
                    THEN 2
                ELSE 1
           END AS FiscalSemester,
           CASE WHEN DATEPART(MM,@StartDate) BETWEEN 1 AND 6
                    THEN DATEPART(YY,@StartDate)
                ELSE DATEPART(YY,@StartDate) + 1
           END AS FiscalYear
           
    SET @StartDate = @StartDate + 1
END
GO

---------------------------------------------------------------------
-- 加上视图的作用是因为在实际的项目开发中,SSAS 的数据源视图所有的表对象
-- 应该都引用视图,这样当数据仓库中维度表或者事实表有小的改动就可以直接在
-- 视图中修改,而可以避免修改 SSAS 项目。
-- 这一点在 SSIS 开发中同样适用,所有在 SSIS 中配置的 SQL 语句都封装在存储
-- 过程中,表封装在视图中。逻辑的修改直接体现在存储过程中,而不会修改 SSIS。
----------------------------------------------------------------------
IF OBJECT_ID(‘vDimDate‘,‘V‘) IS NOT NULL
DROP VIEW vDimDate
GO

CREATE VIEW vDimDate
AS
  -- 可以根据需要实现一些计算列,这些计算列通常也可以在 SSAS 视图中添加。
SELECT DateKey AS ‘DateKey‘,
       FullDate AS ‘FullDate‘,
       [DateName] AS ‘DateName‘,
       CONVERT(VARCHAR(2),DayNumberOfMonth) + ‘ ‘ + EnglishMonthName + ‘ ‘ + CONVERT(CHAR(4), CalendarYear) AS ‘FullDateName‘, -- 1 July 2005
       DayNumberOfWeek AS ‘DayNumberOfWeek‘,
       DayNameOfWeek AS ‘DayNameOfWeek‘,
       DayNumberOfMonth AS ‘DayNumberOfMonth‘,
       DayNumberOfYear AS ‘DayNumberOfYear‘,
       CASE WHEN IsWeekend = 1 THEN ‘Weekend‘
            ELSE ‘Weekday‘
       END AS ‘WeekdayWeekend‘,
       IsLeapYear AS ‘IsLeapYear‘,
       WeekNumberOfYear AS ‘WeekNumberOfYear‘,
       EnglishMonthName AS ‘EnglishMonthName‘,
       EnglishMonthName + ‘ ‘ + CONVERT(CHAR(4),CalendarYear) AS ‘MonthName‘,  -- July 2005
       CalendarYear * 100 + MonthNumberOfYear AS ‘MonthKey‘, -- 200507
       MonthNumberOfYear AS ‘MonthNumberOfYear‘,
       CalendarQuarter AS ‘CalendarQuarter‘,
       CalendarSemester AS ‘CalendarSemester‘,
       CalendarYear AS ‘CalendarYear‘, 
       CalendarYear * 100 + CalendarQuarter AS ‘CalendarQuarterKey‘,  -- 200503
       ‘CY ‘ + CONVERT(CHAR(4),CalendarYear) AS ‘CalendarYearName‘, -- CY 2005
       ‘CY ‘ + CONVERT(CHAR(4),CalendarYear)
             + ‘ Qtr ‘
             + CONVERT(CHAR(1), CalendarQuarter) AS ‘CalendarQuarterName‘, -- CY 2005 Qtr 3 
       FiscalQuarter AS ‘FiscalQuarter‘,
       FiscalSemester AS ‘FiscalSemester‘,
       FiscalYear AS ‘FiscalYear‘,
       FiscalYear * 100 + FiscalQuarter AS ‘FiscalQuarterKey‘, -- 200601
       ‘FY ‘ + CONVERT(CHAR(4), FiscalYear) AS ‘FiscalYearName‘,  -- FY 2006
       ‘FY ‘ + CONVERT(Char(4), FiscalYear) + ‘ Qtr ‘ + Convert(Char(1), FiscalQuarter) AS ‘FiscalQuarterName‘ -- FY 2006 Qtr 1
FROM DimDate
GO

DIM_TIME

IF OBJECT_ID(‘DIM_TIME‘) IS NOT NULL
DROP TABLE DIM_TIME
GO

CREATE TABLE DIM_TIME(
TIME_KEY INT PRIMARY KEY IDENTITY(0,1),
TIME_NAME CHAR(8),
HOUR_NAME CHAR(2),
MILITARY_HOUR CHAR(2),
MINUTE_NAME CHAR(2),
SECOND_NAME CHAR(2),
AM_PM CHAR(2),
STANDARD_TIME CHAR(11)
)

DECLARE @TIME DATETIME

SET @TIME = CONVERT(VARCHAR,‘12:00:00 AM‘,108)

TRUNCATE TABLE DIM_TIME

WHILE @TIME <= ‘11:59:59 PM‘
BEGIN
INSERT INTO DBO.DIM_TIME(TIME_NAME, HOUR_NAME, MILITARY_HOUR, MINUTE_NAME, SECOND_NAME, AM_PM)
SELECT CONVERT(VARCHAR,@TIME,108) TIME_NAME
, CASE
WHEN DATEPART(HOUR,@TIME) > 12 THEN DATEPART(HOUR,@TIME) - 12
ELSE DATEPART(HOUR,@TIME)
END AS HOUR_NAME
, CAST(SUBSTRING(CONVERT(VARCHAR,@TIME,108),1,2) AS INT) MILITARY_HOUR
, DATEPART(MINUTE,@TIME) MINUTE_NAME
, DATEPART(SECOND,@TIME) SECOND_NAME
, CASE
WHEN DATEPART(HOUR,@TIME) >= 12 THEN ‘PM‘
ELSE ‘AM‘
END AS AM_PM

SELECT @TIME = DATEADD(SECOND,1,@TIME)
END

UPDATE DIM_TIME
SET HOUR_NAME = ‘0‘ + HOUR_NAME
WHERE LEN(HOUR_NAME) = 1

UPDATE DIM_TIME
SET MINUTE_NAME = ‘0‘ + MINUTE_NAME
WHERE LEN(MINUTE_NAME) = 1

UPDATE DIM_TIME
SET SECOND_NAME = ‘0‘ + SECOND_NAME
WHERE LEN(SECOND_NAME) = 1

UPDATE DIM_TIME
SET MILITARY_HOUR = ‘0‘ + MILITARY_HOUR
WHERE LEN(MILITARY_HOUR) = 1

UPDATE DIM_TIME
SET STANDARD_TIME = HOUR_NAME + ‘:‘ + MINUTE_NAME + ‘:‘ + SECOND_NAME + ‘ ‘ + AM_PM
WHERE STANDARD_TIME IS NULL
AND HOUR_NAME <> ‘00‘

UPDATE DIM_TIME
SET STANDARD_TIME = ‘12‘ + ‘:‘ + MINUTE_NAME + ‘:‘ + SECOND_NAME + ‘ ‘ + AM_PM
WHERE HOUR_NAME = ‘00‘
GO

时间: 2024-11-04 19:38:21

ssas 时间维度表 sql 代码的相关文章

mysql中生成时间维度表

利用mysql常用日期函数生成时间维度表,效率最高,最简单,无需其他的一些工具支持.生成结果示例如下图: # time span SET @d0 = "2012-01-01"; SET @d1 = "2012-12-31"; SET @date = date_sub(@d0, interval 1 day); # set up the time dimension table DROP TABLE IF EXISTS time_dimension; CREATE T

SSAS的维度表之间的关系只能有一个不能有多个

我们在SSAS中创建维度的时候,有时候可能一个维度需要用到多个表的字段作为维度属性,那么这多个表之间势必存在关联关系,但是切记维度表之间的关联关系有且只能有一个不能有多个,下面我们来看一个例子. 现在我们创建了一个维度Customer,由于该维度中的属性除了用到Customer表的字段以外,还要用到了Geography表的字段,所以Customer用到了两个表. 但是Customer表和Geography表之间现在存在两个依赖关系 其中一个关系是Customer.CustomerKey到Geog

Mysql时间维度表

1.建一张表test_date用来日期的自增长,里面只放一列  ID,写一个存储,插入顺序数字 BEGIN #Routine body goes here... declare id int; set id=1; while id<=6000 do insert into test_date values(id); set id=id+1; end while; END 2. 建立时间维度表,插入记录 SET @d0 = "2000-01-01"; SET @d1 = "

[Java Code] 时间维度循环生成代码片段

public static void main(String[] args) throws ParseException { String str = "20140301"; String str1 = "20140731"; SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); Calendar start = Calendar.getInstance(); Calendar e

【转载】维度表和事实表的区别

免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:知识点滴      原文地址: 维度表, 事实表, 数据仓库, BI...   以前一直对维度表, 事实表, 数据分析, BI等概念等有一些模糊. 这几天的学习终于让这些有了一些眉目了: 维度表示你要对数据进行分析时所用的一个量, 比如你要分析产品销售情况, 你可以选择按类别来进行分析,或按区域来分析. 这样的按..分析就构成一个维度.前面的示例就可以有两个维度:类型和区域.

事实表和维度表(转载)

维度表示你要对数据进行分析时所用的一个量, 比如你要分析产品销售情况, 你可以选择按类别来进行分析,或按区域来分析. 这样的按..分析就构成一个维度.前面的示例就可以有两个维度:类型和区域.另外每个维度还可以有子维度(称为属性),例如类别可以有子类型,产品名等属性. 下面是两个常见的维度表结构: 产品维度表:Prod_id, Product_Name, Category, Color, Size, Price时间维度表:TimeKey, Season, Year, Month, Date 而事实

维度表, 事实表, 数据仓库, BI...

以前一直对维度表, 事实表, 数据分析, BI等概念等有一些模糊. 这几天的学习终于让这些有了一些眉目了: 维度表示你要对数据进行分析时所用的一个量, 比如你要分析产品销售情况, 你可以选择按类别来进行分析,或按区域来分析. 这样的按..分析就构成一个维度.前面的示例就可以有两个维度:类型和区域.另外每个维度还可以有子维度(称为属性),例如类别可以有子类型,产品名等属性.下面是两个常见的维度表结构: 产品维度表:Prod_id, Product_Name, Category, Color, Si

事实表 和 维度表

维度表示你要对数据进行分析时所用的一个量, 比如你要分析产品销售情况, 你可以选择按类别来进行分析,或按区域来分析. 这样的按..分析就构成一个维度.前面的示例就可以有两个维度:类型和区域.另外每个维度还可以有子维度(称为属性),例如类别可以有子类型,产品名等属性. 下面是两个常见的维度表结构: 产品维度表:Prod_id, Product_Name, Category, Color, Size, Price时间维度表:TimeKey, Season, Year, Month, Date 而事实

什么是码表,什么是维度表?

整理业务库的库表字段和字段含义的时候,遇到了码表的概念,码表是什么,和维度表又是什么关系呢? 首先,码表没有明确的定义. 百度搜索码表,可以看到和数据处理相关的解释是:码表是 编码表(或代码表)的简称. 编码表: 是一种代码说明表格. 用来帮助用户明确无解释数据和字符 代码的含义. 例如:性别码表的值为男和女.类似于数据字典. 以性别代码表为例: sex value 1 男 2 女 我想获得一个性别为男的值只需要设置sex.id='1'就可以了. 以上就是关于码表的介绍. 我们不禁想到这和数据仓