日期维度

任何一个数据仓库都应该有一个日期维度。

因为很少有不需要通过日期维度看数据的情况存在。

日期维度的好处是,你可以通过他连接各个事实表,然后在报表端传送报表参数的时候,

直接自动过滤日期维度的相关值,而不需要自己写query.

去掉了西班牙语和法语的那些列。

You know that there are some holes in the sample database, DW2008R2 DIMDATE table.

So I take my own dimdate. Just remove some columns not used.

CREATE TABLE [dbo].[DimDate](

[DateKey] [int] NOT NULL,

[FullDateAlternateKey] [date] NOT NULL,

[DayNumberOfWeek] [tinyint] NOT NULL,

[EnglishDayNameOfWeek] [nvarchar](10) NOT NULL,

[DayNumberOfMonth] [tinyint] NOT NULL,

[DayNumberOfYear] [smallint] NOT NULL,

[WeekNumberOfYear] [tinyint] NOT NULL,

[EnglishMonthName] [nvarchar](10) NOT NULL,

[MonthNumberOfYear] [tinyint] NOT NULL,

[CalendarQuarter] [tinyint] NOT NULL,

[CalendarYear] [smallint] NOT NULL,

[CalendarSemester] [tinyint] NOT NULL,

[FiscalQuarter] [tinyint] NOT NULL,

[FiscalYear] [smallint] NOT NULL,

[FiscalSemester] [tinyint] NOT NULL,

CONSTRAINT [PK_DimDate_DateKey] 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],

CONSTRAINT [AK_DimDate_FullDateAlternateKey] UNIQUE NONCLUSTERED

(

[FullDateAlternateKey] 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

BEGIN TRAN

declare @startdate date = '2005-01-01',

@enddate date = '2015-12-31'

Declare @datelist table (FullDate date)

while @startdate <= @enddate

Begin

Insert into @datelist (FullDate)

Select @startdate

Set @startdate = dateadd(dd,1,@startdate)

end

Insert into dbo.DimDate

(DateKey,

FullDateAlternateKey,

DayNumberOfWeek,

EnglishDayNameOfWeek,

DayNumberOfMonth,

DayNumberOfYear,

WeekNumberOfYear,

EnglishMonthName,

MonthNumberOfYear,

CalendarQuarter,

CalendarYear,

CalendarSemester,

FiscalQuarter,

FiscalYear,

FiscalSemester)

select convert(int,convert(varchar,dl.FullDate,112)) as DateKey,

dl.FullDate,

datepart(dw,dl.FullDate) as DayNumberOfWeek,

datename(weekday,dl.FullDate) as EnglishDayNameOfWeek,

datepart(d,dl.FullDate) as DayNumberOfMonth,

datepart(dy,dl.FullDate) as DayNumberOfYear,

datepart(wk, dl.FUllDate) as WeekNumberOfYear,

datename(MONTH,dl.FullDate) as EnglishMonthName,

Month(dl.FullDate) as MonthNumberOfYear,

datepart(qq, dl.FullDate) as CalendarQuarter,

year(dl.FullDate) as CalendarYear,

case datepart(qq, dl.FullDate)

when 1 then 1

when 2 then 1

when 3 then 2

when 4 then 2

end as CalendarSemester,

case datepart(qq, dl.FullDate)

when 1 then 3

when 2 then 4

when 3 then 1

when 4 then 2

end as FiscalQuarter,

case datepart(qq, dl.FullDate)

when 1 then year(dl.FullDate)

when 2 then year(dl.FullDate)

when 3 then year(dl.FullDate) + 1

when 4 then year(dl.FullDate) + 1

end as FiscalYear,

case datepart(qq, dl.FullDate)

when 1 then 2

when 2 then 2

when 3 then 1

when 4 then 1

end as FiscalSemester

from @datelist dl ;

commit tran;

DimDate populate data的更多相关文章

  1. Bulk Insert:将文本数据(csv和txt)导入到数据库中

    将文本数据导入到数据库中的方法有很多,将文本格式(csv和txt)导入到SQL Server中,bulk insert是最简单的实现方法 1,bulk insert命令,经过简化如下 BULK INS ...

  2. Beginning Scala study note(6) Scala Collections

    Scala's object-oriented collections support mutable and immutable type hierarchies. Also support fun ...

  3. Apache Sqoop - Overview——Sqoop 概述

    Apache Sqoop - Overview Apache Sqoop 概述 使用Hadoop来分析和处理数据需要将数据加载到集群中并且将它和企业生产数据库中的其他数据进行结合处理.从生产系统加载大 ...

  4. 【mysql】使用tpcc-mysql进行压力测试

    Tpcc-mysql是percona基于tpcc衍生出来专用于mysql基准测试的产品 ,可以参见 <高性能MySQL第三版> 一.安装 rpm -Uvh http://dl.fedora ...

  5. An Implementation of Double-Array Trie

    Contents What is Trie? What Does It Take to Implement a Trie? Tripple-Array Trie Double-Array Trie S ...

  6. iphone Dev 开发实例9:Create Grid Layout Using UICollectionView in iOS 6

    In this tutorial, we will build a simple app to display a collection of recipe photos in grid layout ...

  7. Jquery 模板插件 jquery.tmpl.js 的使用方法(1):基本语法,绑定,each循环,ajax获取json数据

    jquery.tmpl.js 是一个模板js  ,主要有2个方法 (1):$.template()方法,将一段script或者是Html编译为模板,例如 $.template('myTemplate' ...

  8. Kafka connect in practice(3): distributed mode mysql binlog ->kafka->hive

    In the previous post Kafka connect in practice(1): standalone, I have introduced about the basics of ...

  9. Mysql怎么判断繁忙 checkpoint机制 innodb的主要参数

    Mysql怎么判断繁忙,innodb的主要参数,checkpoint机制,show engine innodb status   2018年07月13日 15:45:36 anzhen0429 阅读数 ...

随机推荐

  1. Python基础:新式类的属性访问

    一.概述 二.准备工作 1.讨论对象 2.名词解释 三.实例绑定的属性访问 1.获取属性 一般规则 参考源码 示例验证 2.设置属性 一般规则 参考源码 示例验证 3.删除属性 一般规则 参考源码 示 ...

  2. SQL Server数据库ROW_NUMBER()函数使用详解

    SQL Server数据库ROW_NUMBER()函数使用详解 摘自:http://database.51cto.com/art/201108/283399.htm SQL Server数据库ROW_ ...

  3. SQL Server中@@ROWCOUNT的用法

    SQL Server中@@ROWCOUNT返回受上一语句影响的行数,返回值类型为 int 整型. 如果行数大于 20 亿,则需要使用 ROWCOUNT_BIG. @@ROWCOUNT和@@ERROR变 ...

  4. webservice 原理

    webservice 原理1.soap协议,其实就是用http协议来传输xml格式的数据,可以post,get.一般有post.2.服务端有:本地程序代码(也不是dll)和xml格式的文件用来描述dl ...

  5. [PE结构分析] 5.IMAGE_OPTIONAL_HEADER

    结构体源代码如下: typedef struct _IMAGE_OPTIONAL_HEADER { // // Standard fields. // +18h WORD Magic; // 标志字, ...

  6. trie树---(插入、删除、查询字符串)

    HDU   5687 Problem Description 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作:  1.insert : 往神奇字典中插入一个单词  2.delete: 在神奇字 ...

  7. php中的不常用数组函数(一)(数组中元素的键和值对调 array_flip())

    array_flip($arr); //交换数组中的键和值. //如下所示,如果$arr中有相同的值.交换之后 会被旧的覆盖,最后一个有效. /***********array_flip(交换数组中的 ...

  8. No.002:Add Two Numbers

    问题: You are given two linked lists representing two non-negative numbers.  The digits are stored in ...

  9. 【Asphyre引擎】今天终于把精灵demo基本改好了。

    doudou源代码 包含Sprite代码(Sprite还没改完,粒子特效有些问题,但是基本上可以用了) Stage1-1.map  不好意思,漏了地图配置.

  10. 自定义View_2_关于自定义组合View

    自定义View(2) Android当中给我们提供了丰富的UI控件,当然也许满足不了我们的需求,我们就必须学会自定义自己的View,我们怎么算是自定义自己的view呢! 我们会根据原来有的View对V ...