话不多说,之前已经有一篇日志是利用oracle的存储过程生成日期维度表,接下来我们就用sqlserver来实现这个操作,如下面的步骤所示

1:创建日期维度表(Dim_time)

USE [DW]
GO
/****** Object: Table [dbo].[Dim_time] Script Date: 12/19/2015 15:29:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Dim_time](
[the_date] [int] NOT NULL,
[date_name] [nvarchar](30) NULL,
[the_year] [int] NULL,
[year_name] [nvarchar](30) NULL,
[the_quarter] [int] NULL,
[quarter_name] [nvarchar](30) NULL,
[the_month] [int] NULL,
[month_name] [nvarchar](30) NULL,
[the_week] [int] NULL,
[week_name] [nvarchar](30) NULL,
[week_day] [int] NULL,
[week_day_name] [nvarchar](30) NULL,
CONSTRAINT [PK_Dim_time] PRIMARY KEY CLUSTERED
(
[the_date] 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

2:创建生成日期维表数据的存储过程

USE [DW]
GO
/****** Object: StoredProcedure [dbo].[SP_CREATE_TIME_DIMENSION] Script Date: 12/20/2015 12:51:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_CREATE_TIME_DIMENSION]
@begin_date nvarchar(50)='2014-07-01' ,
@end_date nvarchar(50)='2015-12-31'
as
/*
SP_CREATE_TIME_DIMENSION: 生成时间维数据
begin_date: 起始时间
end_date:结束时间
*/
declare
@dDate date=convert(date,@begin_date),
@v_the_date varchar(10),
@v_the_year varchar(4),
@v_the_quarter varchar(2),
@v_the_month varchar(10),
@v_the_month2 varchar(2),
@v_the_week varchar(2),
@v_the_day varchar(10),
@v_the_day2 varchar(2),
@v_week_day nvarchar(10),
@adddays int=1;
WHILE (@dDate<=convert(date,@end_date))
begin
set @v_the_date=convert(char(10),@dDate,112);--key值
set @v_the_year=DATEPART("YYYY",@dDate);--年
set @v_the_quarter=DATEPART("QQ",@dDate);--季度"
set @v_the_month=DATEPART("MM",@dDate);--月份(字符型)
--set @v_the_month2=to_number(to_char(dDate, 'mm'));--月份(数字型)
set @v_the_day=DATEPART("dd",@dDate);--日(字符型)
--set @v_the_day2=to_char(dDate, 'dd');
set @v_the_week=DATEPART("WW",@dDate);--年的第几周
set @v_week_day=DATEPART("DW",@dDate); --星期几
insert into Dim_time(the_date,date_name,the_year,year_name,the_quarter,quarter_name,the_month,month_name,the_week,week_name,week_day,week_day_name)
values(
@v_the_date,
convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_month)+'月'+convert(nvarchar(10),@v_the_day)+'日',
@v_the_year,
convert(nvarchar(10),@v_the_year)+'年',
@v_the_quarter,
convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_quarter)+'季度',
case when @v_the_month>=10 then
convert(int,(convert(nvarchar(10),@v_the_year)+convert(nvarchar(10),@v_the_month)))
else convert(int,convert(nvarchar(10),@v_the_year)+'0'+convert(nvarchar(10),@v_the_month)) end,
convert(nvarchar(10),@v_the_year)+'年'+convert(nvarchar(10),@v_the_month)+'月',
@v_the_week
,'第'+convert(nvarchar(10),@v_the_week)+'周',
@v_week_day,
case @v_week_day-1
when 1 then '星期一'
when 2 then '星期二'
when 3 then '星期三'
when 4 then '星期四'
when 5 then '星期五'
when 6 then '星期六'
when 0 then '星期日'
else '' end
);
set @dDate=dateadd(day,@adddays,@dDate);
continue
if @dDate=dateadd(day,-1,convert(date,@end_date))
break
end
 
 

3:执行存储过程

USE [DW]
GO DECLARE @return_value int EXEC @return_value = [dbo].[SP_CREATE_TIME_DIMENSION] SELECT 'Return Value' = @return_value GO

4:查看2015年12月份的维度表内容如下图所示,大功告成

5:注意和扩展

a:可以根据思路运用到各种数据库当中,例如本文就是根据oracle的procedure改造过来的

b:注意上表中的最后一个字段week_day星期几的字段,星期一 至 星期天依次为 2,3,4,5,6,7,1

Sqlserver存储过程生成日期维度的更多相关文章

  1. Oracle存储过程生成日期维度

    在数据仓库的创建过程中,往往需要创建日期维度来为以后的数据分析来服务. 方面从多个日期角度: 如:年-月-日,年-季度-月-日,年-周-日 创建表的脚本如下(存储过程的创建过程中有一步操作是向time ...

  2. C# 生成日期维度值

    1. 时间维度表结构 /*==============================================================*/ /* Table: dim_date_day ...

  3. SQL生成日期维度(到小时)

    #建表语句: CREATE TABLE [dbo].[Dim_日期3]( ) NOT NULL, [年] [int] NULL, ) NULL, ) NULL, ) NULL, ) NULL, ) N ...

  4. SQLserver 存储过程生成任意进制/顺序流水号

    ALTER    PROCEDURE [dbo].[TentoSerial] @num int, @ret nvarchar(10) output AS declare @StringXL nvarc ...

  5. DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension)

    DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate TIME Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★★★★★(5星) 这个时间系列想 ...

  6. SqlServer存储过程学习笔记(增删改查)

    * IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...

  7. SqlServer存储过程(增删改查)

    * IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值. CREATE PROCEDURE [dbo].[PR_NewsAffiche_AddNewsEntity] ( ...

  8. PowerBI 应用时间智能(生成日期表)

    简介 Power BI Desktop -是一款由微软发布的自助式商业智能工具,功能强大.易于使用.其中还可以通过微软云连多个数据源并且使用数据源来创建可视化表盘. 但是几乎所有的BI都需要展示如何随 ...

  9. DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate Date Dimension)

    跟大家的交流是我的动力. :) DAX/PowerBI系列 - 关于时间系列 - 如何用脚本生成时间维度 (Generate Date Dimension) 难度: ★☆☆☆☆(1星) 适用范围: ★ ...

随机推荐

  1. python opencv3 读写图像文件

    git: https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 # 读取一张图片, 第二个参数可选 image = ...

  2. QQ怎么 发送 已经录好的视频

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha QQ发送 已经录好的视频 直接放过去,对方是需要下载的. 只有通过QQ录制的,才是直接就 ...

  3. HTML5区块和大纲算法

    原文链接: Using HTML sections and outlines - Mozilla Developer Network 每集HTML5+CSS3网页布局教程-2大纲算法 HTML5标准带 ...

  4. 高并发系列之——负载均衡,web负载均衡

    1 前言 负载均衡,一般包含两方面的含义.一方面是,将单一的重负载分担到多个网络节点上做并行处理,每个节点处理结束后将结果汇总返回给用户,这样可以大幅提高网络系统的处理能力: 第二个方面的含义是,将大 ...

  5. Substring with Concatenation of All Words 题解

    题意 You are given a string, s, and a list of words, words, that are all of the same length. Find all ...

  6. Codeforces Round #356 (Div. 2) A. Bear and Five Cards 水题

    A. Bear and Five Cards 题目连接: http://www.codeforces.com/contest/680/problem/A Description A little be ...

  7. Sql 先进先出计算积分

    先建表,插入测试数据 --正积分表 CREATE table tb1 ( ) NOT NULL, ) NOT NULL, ) NULL, [point] [int] NULL ) ) ) ) ) ) ...

  8. Set常用子类特点

    HashSet:       重写   hashCode和equals方法                                        特点:无序,唯一      底层结构是:    ...

  9. mysql---总体备份和增量备份

    总体备份: 对整张表或者整个数据库甚至全部数据库进行备份. 增量备份: 对某一范围内的数据进行备份. 1.总体备份: 对表进行备份: 针对存储引擎为myisam的表,能够直接复制frm.myd.myi ...

  10. 做iphone5适配时,一定要做好图片大小的调整。