Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Fiscal Dates Expand / Collapse
Author
Message
Posted Sunday, September 2, 2012 8:54 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, March 18, 2014 5:11 PM
Points: 55, Visits: 342
My company wants to create a Financial Calendar Table which contains only the Fiscal Dates. The requirements is ti populate it with the following columns:
- DateKey
- Fiscal_Year
- Fiscal_Month
- Fiscal_Week
- Fiscal Week_Start_Date
- Fiscal_Week_End_Date

The rules for these columns are:
- DateKey has to be in the format YYYYMMDD
- Weeks begin from Monday - Sunday
- Financial Year begins on 01/Jul
- Financial Week begins from Monday , which means if 01/Jul falls on any other days, then we have to take the Monday of that week as the beginning of the financial week. e.g. If 01/Jul falls on Wednesday, then the beginning of week is 29/06

Can someone help me with this script?
Post #1353313
Posted Monday, September 3, 2012 12:09 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
rka (9/2/2012)
DateKey has to be in the format YYYYMMDD


In SQL Server, that's a pretty insane requirement because that would make a character based date. Since you're using SQL Server 2008, you should either use DATETIME or DATE datatypes for such a thing.

I strongly recommmend you talk to the people that designed the table and suggest that they should probably change it.

As for actually building the table... what have you tried that's not working?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1353354
Posted Monday, September 3, 2012 8:36 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 1,945, Visits: 2,922
>> My company wants to create a Financial Calendar Table which contains only the Fiscal Dates. <<

I hope not! Fiscal calendars have to come back to the Common Era and lawful (civil) calendars.

>> The requirements are populate it with the following columns:
- DateKey
- Fiscal_Year
- Fiscal_Month
- Fiscal_Week
- Fiscal Week_Start_Date
- Fiscal_Week_End_Date <<

Again, I hope not! The idea of a “date_key” is soooo wrong so many ways. A date is a temporal unit of measurement. The attribute property “_key” is meta data in violation of ISO-11179, basic data modeling and common sense.

Let me make a suggestion then explain it.

CREATE TABLE Fiscal_Calendar
(cal_date DATE NOT NULL PRIMARY KEY,
fiscal_date CHAR(9) NOT NULL
CHECK (fiscal_date LIKE
'[12][0-9][0-9][0-9]F[0-5][0-9]-[1-7]'),
etc);

>> DateKey [sic] has to be in the format YYYYMMDD <<

Use the calendar date. We do not care about the internal format for dates, numbers, or strings. You can use that format in the presentations layer. But the ISO-8601 format used in SQL and the rest of the ISO Standards is 'yyyy-mm-dd', so your need a good reason to go into a local dialect.

>> - Weeks begin from Monday – Sunday <<

Of course, and ISO says the days are numbered 1 to 7. That is standard.

>> - Financial Year begins on July 01.
- Financial Week begins from Monday, which means if July 01 falls on any other days, then we have to take the Monday of that week as the beginning of the financial week. e.g. If July 01 falls on Wednesday, then the beginning of week is June 29 <<

So you have a fiscal calender based on weeks. This is one of the 150+ options in the GAAP; I am not user with your narrative if you will have fiscal weeks that are not 7 days (I have seen 4 and 12 days) to align the calendar and fiscal years.

I have modified the ISO-8601 week-within-year format to use 'F' as a marker for Fiscal weeks, as opposed to the usual 'W' for calendar weeks. Thus '2012F01-1' is the first fiscal week of 2012, and a Monday. You get the parts with substrings in computed columns, VIEWs and queries.

Unfortunately, you did not define a fiscal month. In some financial calendars a month is always 30 days and a year is always 360 days with inter-calendaral days to make up the differences.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1353682
Posted Tuesday, September 4, 2012 9:35 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, July 18, 2014 9:09 AM
Points: 870, Visits: 2,385
Jeff Moden (9/3/2012)
rka (9/2/2012)
DateKey has to be in the format YYYYMMDD


In SQL Server, that's a pretty insane requirement because that would make a character based date. Since you're using SQL Server 2008, you should either use DATETIME or DATE datatypes for such a thing.

I strongly recommmend you talk to the people that designed the table and suggest that they should probably change it.

As for actually building the table... what have you tried that's not working?


Jeff I agree its pretty insane,

But I've seen this especially if the YYYYMMDD is converted to an INT eg 20120101 = 20,120,101.

Its been a frequent topic of discussion on various DW projects that I've worked on, prior to the Date Data type most of the calendar dims used the DateTime with the PK/SK column a CAST(CalendarDate as Int), again a pretty nasty way of doing this especially if you have a time element due to the rounding issues.

However, since 2008 I've been converted to Date Data type as the PK/SK and it seems to work very well.


_________________________________________________________________________
SSC Guide to Posting and Best Practices
Post #1353999
Posted Tuesday, September 4, 2012 1:17 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 6:23 AM
Points: 1,945, Visits: 2,922
But I've seen this especially if the YYYYMMDD is converted to an INT eg 20120101 = 20,120,101. .. , since 2008 I've been converted to Date Data type as the PK/SK and it seems to work very well.


Could be worse; I have see FLOAT, GUID and weird text strings.


Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Post #1354141
Posted Tuesday, September 4, 2012 2:17 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
Jason-299789 (9/4/2012)
Jeff Moden (9/3/2012)
rka (9/2/2012)
DateKey has to be in the format YYYYMMDD


In SQL Server, that's a pretty insane requirement because that would make a character based date. Since you're using SQL Server 2008, you should either use DATETIME or DATE datatypes for such a thing.

I strongly recommmend you talk to the people that designed the table and suggest that they should probably change it.

As for actually building the table... what have you tried that's not working?


Jeff I agree its pretty insane,

But I've seen this especially if the YYYYMMDD is converted to an INT eg 20120101 = 20,120,101.

Its been a frequent topic of discussion on various DW projects that I've worked on, prior to the Date Data type most of the calendar dims used the DateTime with the PK/SK column a CAST(CalendarDate as Int), again a pretty nasty way of doing this especially if you have a time element due to the rounding issues.

However, since 2008 I've been converted to Date Data type as the PK/SK and it seems to work very well.


NP. Thanks for the feedback. I have to ask again, though... what have you tried that isn't working in your efforts to build this table?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1354173
Posted Tuesday, September 4, 2012 7:46 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
Jason-299789 (9/4/2012)
Jeff Moden (9/3/2012)
rka (9/2/2012)
DateKey has to be in the format YYYYMMDD


In SQL Server, that's a pretty insane requirement because that would make a character based date. Since you're using SQL Server 2008, you should either use DATETIME or DATE datatypes for such a thing.

I strongly recommmend you talk to the people that designed the table and suggest that they should probably change it.

As for actually building the table... what have you tried that's not working?


Jeff I agree its pretty insane,

But I've seen this especially if the YYYYMMDD is converted to an INT eg 20120101 = 20,120,101.

Its been a frequent topic of discussion on various DW projects that I've worked on, prior to the Date Data type most of the calendar dims used the DateTime with the PK/SK column a CAST(CalendarDate as Int), again a pretty nasty way of doing this especially if you have a time element due to the rounding issues.

However, since 2008 I've been converted to Date Data type as the PK/SK and it seems to work very well.

A "DW" is a data warehouse which is an OLAP-type database, and these rules and keys might make sense there, because an OLAP database has different goals than an OLTP database, and thus a different (though related) modelling methodology, and therefore a different set of normal rules.

An OLAP database is designed to facilitate reporting, at the expense of other things, like diskspace and being able to maintain the data in real-time. OLTP databases on the other hand are designed to facilitate real-time data updates while insuring consistency and still be able to report on the data.

Because of these differences in goals, OLAP databases will frequently have highly redundant multi-discriminating key structures, like the one you suggest, because they can greatly facilitate reporting and summarization. But they are an anethema to an OLTP database because they introduce all kinds of problems with maintaining the consistency of the data and keys when trying to incrementally keep it up-to-date in real time.


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #1354294
Posted Tuesday, September 4, 2012 10:50 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:11 AM
Points: 36,995, Visits: 31,516
RBarryYoung (9/4/2012)
Jason-299789 (9/4/2012)
Jeff Moden (9/3/2012)
rka (9/2/2012)
DateKey has to be in the format YYYYMMDD


In SQL Server, that's a pretty insane requirement because that would make a character based date. Since you're using SQL Server 2008, you should either use DATETIME or DATE datatypes for such a thing.

I strongly recommmend you talk to the people that designed the table and suggest that they should probably change it.

As for actually building the table... what have you tried that's not working?


Jeff I agree its pretty insane,

But I've seen this especially if the YYYYMMDD is converted to an INT eg 20120101 = 20,120,101.

Its been a frequent topic of discussion on various DW projects that I've worked on, prior to the Date Data type most of the calendar dims used the DateTime with the PK/SK column a CAST(CalendarDate as Int), again a pretty nasty way of doing this especially if you have a time element due to the rounding issues.

However, since 2008 I've been converted to Date Data type as the PK/SK and it seems to work very well.

A "DW" is a data warehouse which is an OLAP-type database, and these rules and keys might make sense there, because an OLAP database has different goals than an OLTP database, and thus a different (though related) modelling methodology, and therefore a different set of normal rules.

An OLAP database is designed to facilitate reporting, at the expense of other things, like diskspace and being able to maintain the data in real-time. OLTP databases on the other hand are designed to facilitate real-time data updates while insuring consistency and still be able to report on the data.

Because of these differences in goals, OLAP databases will frequently have highly redundant multi-discriminating key structures, like the one you suggest, because they can greatly facilitate reporting and summarization. But they are an anethema to an OLTP database because they introduce all kinds of problems with maintaining the consistency of the data and keys when trying to incrementally keep it up-to-date in real time.


BWAA-HAAA!!!! Is that the long version for "storing dates as INTs or VARCHARs sucks"???


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1354313
Posted Wednesday, September 5, 2012 5:48 PM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
Jeff Moden (9/4/2012)
RBarryYoung (9/4/2012)
Jason-299789 (9/4/2012)
Jeff Moden (9/3/2012)
rka (9/2/2012)
DateKey has to be in the format YYYYMMDD


In SQL Server, that's a pretty insane requirement because that would make a character based date. Since you're using SQL Server 2008, you should either use DATETIME or DATE datatypes for such a thing.

I strongly recommmend you talk to the people that designed the table and suggest that they should probably change it.

As for actually building the table... what have you tried that's not working?


Jeff I agree its pretty insane,

But I've seen this especially if the YYYYMMDD is converted to an INT eg 20120101 = 20,120,101.

Its been a frequent topic of discussion on various DW projects that I've worked on, prior to the Date Data type most of the calendar dims used the DateTime with the PK/SK column a CAST(CalendarDate as Int), again a pretty nasty way of doing this especially if you have a time element due to the rounding issues.

However, since 2008 I've been converted to Date Data type as the PK/SK and it seems to work very well.

A "DW" is a data warehouse which is an OLAP-type database, and these rules and keys might make sense there, because an OLAP database has different goals than an OLTP database, and thus a different (though related) modelling methodology, and therefore a different set of normal rules.

An OLAP database is designed to facilitate reporting, at the expense of other things, like diskspace and being able to maintain the data in real-time. OLTP databases on the other hand are designed to facilitate real-time data updates while insuring consistency and still be able to report on the data.

Because of these differences in goals, OLAP databases will frequently have highly redundant multi-discriminating key structures, like the one you suggest, because they can greatly facilitate reporting and summarization. But they are an anethema to an OLTP database because they introduce all kinds of problems with maintaining the consistency of the data and keys when trying to incrementally keep it up-to-date in real time.


BWAA-HAAA!!!! Is that the long version for "storing dates as INTs or VARCHARs sucks"???


8^Þ

(heh)


-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #1354960
Posted Wednesday, September 5, 2012 6:13 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:50 AM
Points: 5,595, Visits: 24,985
These two articles might give you a method of doing what you desire.

http://www.sqlservercentral.com/articles/function/67046/

http://www.sqlservercentral.com/articles/function/68323/



If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #1354966
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse