<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sqlserver007 &#187; cte</title>
	<atom:link href="http://www.sqlserver007.com/tag/cte/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sqlserver007.com</link>
	<description>Blog on SQL SERVER and Business Intelligence  by Vivekanand Serou</description>
	<lastBuildDate>Fri, 21 May 2010 11:37:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SQL SERVER :Date and Time Dimensions in T-SQL using CTE</title>
		<link>http://www.sqlserver007.com/2010/01/20/sql-server-date-and-time-dimensions-in-t-sql-using-cte/</link>
		<comments>http://www.sqlserver007.com/2010/01/20/sql-server-date-and-time-dimensions-in-t-sql-using-cte/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 13:27:20 +0000</pubDate>
		<dc:creator>Vivek</dc:creator>
				<category><![CDATA[Business Intelligence]]></category>
		<category><![CDATA[SSAS]]></category>
		<category><![CDATA[TSQL Development]]></category>
		<category><![CDATA[cte]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.sqlserver007.com/?p=506</guid>
		<description><![CDATA[Everytime we create a cube for analysis services Date and Time Dimensions are needed to slice and dice the historical Data. Sql Server Analysis Services uses the Time Intelligence Features to populate the date and time. Its posted in detail in this blog. However we need to use them in our relational Databases to do [...]]]></description>
			<content:encoded><![CDATA[<p>Everytime we create a cube for analysis services Date and Time Dimensions are needed to slice and dice the historical Data. Sql Server Analysis Services uses the Time Intelligence Features to populate the date and time. Its posted in detail in this <a href="http://www.mssqltips.com/tip.asp?tip=1454">blog</a>.</p>
<p>However we need to use them in our relational Databases to do some adhoc reporting. I always use the following CTE&#8217;s to create Date and Time Dimensions. The good thing about CTE is you can recursively loop through the data till you get the desired result. </p>
<p><strong>DATE DIMENSION :</strong></p>
<p>I got the Date Dimension idea originally from <a href="http://consultingblogs.emc.com/jamiethomson/archive/2007/01/11/T_2D00_SQL_3A00_-Generate-a-list-of-dates.aspx">JamieT (Jamie Thomson,MVP SQL Server) &#8216;s blog.</a></p>
<pre>
with DateCTE  as
(
	select cast ('01-Jan-2009' as datetime)   Datevalue
	union all
	select datevalue + 1
	from DateCTE where datevalue + 1 < = '31-Dec-2020'
)
select cast(datevalue as int ) as DateID
,datename(year,DateValue) as [Year]
,dateName(Month,DateValue) as [Month]
,datename(d,datevalue) as [datenumber]
,datename(Week,Datevalue) as [Week]
,datename(DW,Datevalue) as [Day]
,Datevalue  as [Date]
from DateCTE
order by Datevalue
option (maxrecursion 0)
</pre>
<p><strong>TIME DIMENSION :</strong></p>
</pre>
<pre>  with HourCTE(Hour,Minute) as
  (
  select 0 as Hour , 0 as Minute
  union all
  select Hour , Minute + 1
  from HourCTE
  where Minute + 1 < 60
  ),
   MinuteCTE(Hour,minute) as
  (
    select Hour, Minute from HourCTE
	union all
	select Hour + 1 ,Minute from MinuteCTE
	where Hour + 1 < 24
)
select * from minuteCTE
order by Hour ,Minute </pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.sqlserver007.com/2010/01/20/sql-server-date-and-time-dimensions-in-t-sql-using-cte/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
