Sqlserver存儲過程生成日期維度


話不多說,之前已經有一篇日志是利用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM