Solved

Write a TSQL to generate a date for a BI date table

Posted on 2014-10-30
5
197 Views
Last Modified: 2014-11-04
How to run a TSQL to generate the following records for a particular year ?

YEAR      FISALPERIOD   PERIOD      QUARTER      MONTH          WEEKNO      DATE
2014      8                        11                      2014/Q3              2014/11          45               2014-11-05 00:00:00.000
0
Comment
Question by:AXISHK
  • 3
5 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 40413081
One record per week?  per day?
0
 
LVL 14

Accepted Solution

by:
Christopher Gordon earned 350 total points
ID: 40413094
If it's daily, something like this would work (I'm assuming your fiscal period occurs three months before calendar period):

declare @i int = 1
declare @currentDate date = '1/1/2014'

declare @time_dim table (calendarYear int, fiscalPeriod int, calendarPeriod int, calendarQuarter varchar(10), calendarMonth varchar(10), weekno int, calendarDate datetime)

while @i <= 365
begin

	insert into @time_dim (calendarYear, fiscalPeriod, calendarPeriod, calendarQuarter, calendarMonth, weekno, calendarDate)
	
	values (
			YEAR(@currentDate)
		,	Month(DATEADD(m, -3, @currentDate))
		,	MONTH(@currentDate)
		,	cast(year(@currentDate) as char(4)) + '/Q' + cast(DATEPART(quarter, @currentDate) as CHAR(1))
		,	cast(year(@currentDate) as char(4)) + '/' + cast(DATEPART(month, @currentDate) as CHAR(2))
		,	DATEPART(week, @currentDate), @currentDate)

	set @i = @i + 1
	
	set @currentDate = dateadd(day, 1, @currentDate)

end

select * from @time_dim

Open in new window

0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 150 total points
ID: 40413170
Looks like you're getting an answer above, but just to throw it out here's an article on Build your own calendar table to perform complex date expressions with source code that pulls this off by day.  Scroll down to 'Now we're done modifying the table, which will look like this ' and you'll see how it looks similar to what you're asking.
0
 

Author Comment

by:AXISHK
ID: 40421128
Not correct, The record should be like the value below,


YEAR      PERIOD      QUARTER      MONTH      WEEKNO            DATE_IDX
2014      1               2014/Q4              2015/01      1                    2015-01-01 00:00:00.000
2014      4               2014/Q1              2014/04      14                    2014-04-01 00:00:00.000
2014      12               2014/Q3        2014/12      53                    2014-12-31 00:00:00.000
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 40423047
I added a dateadd(m, -3, @currentDate) in the part of the code that calculates the quarter field

declare @i int = 1
declare @currentDate date = '1/1/2014'

declare @time_dim table (calendarYear int, fiscalPeriod int, calendarPeriod int, calendarQuarter varchar(10), calendarMonth varchar(10), weekno int, calendarDate datetime)

while @i <= 365
begin

	insert into @time_dim (calendarYear, fiscalPeriod, calendarPeriod, calendarQuarter, calendarMonth, weekno, calendarDate)
	
	values (
			YEAR(@currentDate) --calendar year 
		,	Month(DATEADD(m, -3, @currentDate)) -- fiscal period
		,	MONTH(@currentDate) -- calendar period
		,	cast(year(DATEADD(m, -3, @currentDate)) as char(4)) + '/Q' + cast(DATEPART(quarter, DATEADD(m, -3, @currentDate)) as CHAR(1)) -- 
		,	cast(year(@currentDate) as char(4)) + '/' + cast(DATEPART(month, @currentDate) as CHAR(2))
		,	DATEPART(week, @currentDate), @currentDate)

	set @i = @i + 1
	
	set @currentDate = dateadd(day, 1, @currentDate)

end


select * from @time_dim

                                          

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now