![]() |
Database »
Database »
SQL Server
Intermediate
License: The Code Project Open License (CPOL)
Creating Time Dimension in Microsoft Analysis ServicesBy Igor KrupitskyUnlike Date Dimension (with Year, Month, Date), Time Dimension (with Hour, Minutes, and Seconds) is not automatically created by SSAS (Microsoft SQL Server Analysis Services). This article explains how to create and use Time Dimension in Microsoft SQL Server Analysis Services. |
SQL, WindowsSQL 2000, SQL 2005, DBA, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

Sometimes, you need to create a report on a time level (Hour, Minutes, and Seconds). For a relational report, you can use the DATEPART() function to extract time information out of a DateTime column. However, doing this is difficult for an OLAP report. This article explains how to create a Time Dimension table and use it inside SSAS.
To understand this article, you need to understand the basics of dimensional modeling and Microsoft SQL Server Analysis Services.
The code (zip file) is composed of four scripts:
CREATE TABLE statement creates the DimTime table.The Populate DimTime.sql script looks like this:
delete from DimTime
declare @DimTimeKey int, @Date datetime, @AM char(2),
@hour24 tinyint, @hour tinyint, @minute tinyint, @second int
set @DimTimeKey = 0
while @DimTimeKey < (60*60*24)
begin
set @DimTimeKey = @DimTimeKey + 1
set @Date = DATEADD(second,@DimTimeKey,convert(datetime, '1/1/2007'))
set @AM = right(convert(varchar,@Date,109),2)
set @hour24 = DATEPART(hour, @Date)
set @hour = case when @AM = 'PM' then @hour24 - 12 else @hour24 end
set @minute = DATEPART(minute, @Date)
set @second = DATEPART(second, @Date)
insert into dbo.DimTime(DimTimeKey,Time,Time24,HourName, _
MinuteName,MinuteKey,Hour,Hour24,Minute,Second,AM)
select @DimTimeKey,
Time = right('0' + convert(varchar,@hour),2)
+ ':' + right('0' + convert(varchar,@minute),2)
+ ':' + right('0' + convert(varchar,@second),2) + ' ' + @AM,
Time24 = convert(varchar,@Date,108),
HourName = right('0' + convert(varchar,@hour),2) + ' ' + @AM,
MinuteName = right('0' + convert(varchar,@hour),2)
+ ':' + right('0' + convert(varchar,@minute),2)+ ' ' + @AM,
MinuteKey = (@hour24*60) + @minute,
@hour, @hour24, @minute, @second, @AM
end
| You must Sign In to use this message board. | ||||||||
|
||||||||
|
||||||||
|
||||||||
|
||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 6 May 2008 Editor: Smitha Vijayan |
Copyright 2008 by Igor Krupitsky Everything else Copyright © CodeProject, 1999-2009 Web15 | Advertise on the Code Project |