Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Calendar Tables III - Changing Business Rules


Calendar Tables III - Changing Business Rules

Author
Message
tfifield
tfifield
SSC Eights!
SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)

Group: General Forum Members
Points: 991 Visits: 2890
Comments posted to this topic are about the item Calendar Tables III - Changing Business Rules
abacrotto
abacrotto
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 61
Hey!!! I liked the way you worked out the problem. I have a little comment for some of the code. You are using while loops in some of the code that you can replace with pure SQL code.

The example is when creating the table with the weeks. The program cycles and inserts data into a table. I thinks this algorithm would help you in two ways. It's faster and easier to read.


DECLARE @FromDate DATETIME, @ToDate DATETIME
SELECT @FromDate = '20091228', @ToDate = '20111231'

-- Number of days between the from and to dates
DECLARE @Diff INT
SELECT @Diff = DATEDIFF(DAY, @FromDate, @ToDate)

SELECT TOP (@Diff / 7 + 1)
-- ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) gives us the week number
-- First day of week gets calculated with offset 0
DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) * 7 + 0, @FromDate) AS DateStart,
-- Last day of the week gets calculates with offset 6
DATEADD(DAY, (ROW_NUMBER() OVER (ORDER BY C1.OBJECT_ID, C1.COLUMN_ID) - 1) * 7 + 6, @FromDate) AS DateEnd
FROM
Sys.columns C1, Sys.columns C2


As you can see I base it in ROW_NUMBER() function so, it will just run on SQL Server 2005 or above.

Regards.
Ariel from Argentina.
tfifield
tfifield
SSC Eights!
SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)

Group: General Forum Members
Points: 991 Visits: 2890
abacrotto,
Thank you for your reply. Yes, the creation of the table could be done set-based, however, it is only done once so I don't pay much attention to performance for just creating the Calendar Table. Also, When I create a Calendar table I usually put more data into it. For example I might add '2010 - Week 1' and so forth as a column for display purposes. For a daily table I might add 'Monday', 'Tuesday', etc., so I can put whatever I want for a day name rather than depend on the DATENAME() function. The while loop makes it easier to add these information only type columns.

I hope you find calendar tables useful.
Todd Fifield
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21099 Visits: 18259
Thanks for continuing the series.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Nicole Bowman
Nicole Bowman
SSC Veteran
SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)SSC Veteran (209 reputation)

Group: General Forum Members
Points: 209 Visits: 1615
Thanks for the clear explanation. It is good to see a clean, simple way to implement a change in business rules without having to change reams of front end code. It shows good planning, and understanding of the business needs, and reminds me about the importance of the design phase of my projects - measure twice, cut once!

Cheers,

Nicole Bowman

Nothing is forever.
ziangij
ziangij
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2910 Visits: 374
thanks for this post :-)

One question - will it be good to add a column instead - say IsWeekEnd, IsWeekStart. Will it fare better or worse than the current query ?

Thanks in advance.
James A Skipwith
James A Skipwith
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 640
Todd,

Nice article. I also have large calendar tables to deal with - as one of my clients uses a rolling four week cycle starting the first saturday before February the 2nd, so as well as fiscal weeks to deal with I also have week 1-4 four each four week block! I too do not worry about optimising the SQL to populate this, as performance is not a considuration here, accuracy is.

Plus how come you get away with one page of forum questions where I have 4+ each time!

Look forward to more articles.

James

James
MCM [@TheSQLPimp]
tfifield
tfifield
SSC Eights!
SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)

Group: General Forum Members
Points: 991 Visits: 2890
CirquedeSQLeil (2/16/2011)
Thanks for continuing the series.

Jason,
That means a lot to me coming from you.
Todd Fifield
tfifield
tfifield
SSC Eights!
SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)

Group: General Forum Members
Points: 991 Visits: 2890
Nicole Bowman (2/16/2011)
Thanks for the clear explanation. It is good to see a clean, simple way to implement a change in business rules without having to change reams of front end code. It shows good planning, and understanding of the business needs, and reminds me about the importance of the design phase of my projects - measure twice, cut once!

Cheers,

Nicole,
I love that little quote - measure twice, cut once!

Glad you enjoyed the article.
Todd Fifield
tfifield
tfifield
SSC Eights!
SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)SSC Eights! (991 reputation)

Group: General Forum Members
Points: 991 Visits: 2890
ziangij (2/16/2011)
thanks for this post :-)

One question - will it be good to add a column instead - say IsWeekEnd, IsWeekStart. Will it fare better or worse than the current query ?

Thanks in advance.

Ziangij,
I've tried it both ways. Usually just using the date itself works better and you don't have to add any unnecessary flags to the tables.
Todd Fifield
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search