March 30, 2012 at 4:06 am
I'm looking for some input and comments/ideas.
I've got a transaction table (orders) which contains an OrderDate column, which is a datetime column.
I want to join this table to a (daily) calendar table on date and clearly I need to somehow deal with the fact that OrderDate may have a time component (which I am not concerned with, for the purposes of this exercise).
I know that I could do something like
where Calendar.Date >= cast(OrderDate as date)
and Calendar.Date < dateadd(day, 1, cast(OrderDate as date))
But it seems a little cumbersome, and potentially quite expensive.
Another idea is that I add a computed column to the OrderDate table which is Cast(OrderDate as Date) and then index it. I could then join the calendar table directly to this. What do people think of this idea?
Thanks for any input.
March 30, 2012 at 6:36 am
...
Another idea is that I add a computed column to the OrderDate table which is Cast(OrderDate as Date) and then index it. I could then join the calendar table directly to this. What do people think of this idea?
...
I think that above will give you the best performance of your JOIN, do this if situation allows (as it will affect performance of inserting into this table...)
March 30, 2012 at 6:43 am
Can you change the Calendar table? If so, you could add a computed persisted column " NextDay as dateadd(day, 1, Calendar.Date) "
Your join would then be
where OrderDate >= Calendar.Date
and OrderDate < Calendar.NextDay
I'm afraid I don't know how this will perform though
____________________________________________________
Deja View - The strange feeling that somewhere, sometime you've optimised this query before
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537March 30, 2012 at 6:49 am
If you can make a small mod to your Calendar table... I usually create a Calendar table with 2 date columns to easily solve such an eventuality. The first column is, of course, the date for the current row. The second column (which most people don't have) is the date of the next day.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 30, 2012 at 7:06 am
Jeff Moden (3/30/2012)
If you can make a small mod to your Calendar table... I usually create a Calendar table with 2 date columns to easily solve such an eventuality. The first column is, of course, the date for the current row. The second column (which most people don't have) is the date of the next day.
Jeff, that's a great idea - implementing it now, with a small tweak. Column 2 will be date1 + 23:59:59.997 so that I can use 'BETWEEN date1 and date2' in my WHERE clause.
Many thanks
Phil
March 30, 2012 at 7:20 am
Phil Parkin (3/30/2012)
Jeff Moden (3/30/2012)
If you can make a small mod to your Calendar table... I usually create a Calendar table with 2 date columns to easily solve such an eventuality. The first column is, of course, the date for the current row. The second column (which most people don't have) is the date of the next day.Jeff, that's a great idea - implementing it now, with a small tweak. Column 2 will be date1 + 23:59:59.997 so that I can use 'BETWEEN date1 and date2' in my WHERE clause.
Many thanks
Phil
Are you going to add another column to handle using BETWEEN when you move to SQL Server 2008 or later and find yourself using the DATETIME2 data type? I would go with Jeff's suggestion and use >= and < in the where clause.
March 30, 2012 at 7:27 am
Lynn Pettis (3/30/2012)
Phil Parkin (3/30/2012)
Jeff Moden (3/30/2012)
If you can make a small mod to your Calendar table... I usually create a Calendar table with 2 date columns to easily solve such an eventuality. The first column is, of course, the date for the current row. The second column (which most people don't have) is the date of the next day.Jeff, that's a great idea - implementing it now, with a small tweak. Column 2 will be date1 + 23:59:59.997 so that I can use 'BETWEEN date1 and date2' in my WHERE clause.
Many thanks
Phil
Are you going to add another column to handle using BETWEEN when you move to SQL Server 2008 or later and find yourself using the DATETIME2 data type? I would go with Jeff's suggestion and use >= and < in the where clause.
Err, no Thanks for the insight Lynn.
March 30, 2012 at 7:47 am
Phil Parkin (3/30/2012)
Jeff Moden (3/30/2012)
If you can make a small mod to your Calendar table... I usually create a Calendar table with 2 date columns to easily solve such an eventuality. The first column is, of course, the date for the current row. The second column (which most people don't have) is the date of the next day.Jeff, that's a great idea - implementing it now, with a small tweak. Column 2 will be date1 + 23:59:59.997 so that I can use 'BETWEEN date1 and date2' in my WHERE clause.
Many thanks
Phil
Just to emphasize what Lynn has already stated... using 23:59:59.997 is both an over complication and an opportunity to miss data with the new datatypes. The dates in your calendar table should be "whole" dates (midnight time) and you should us the ">= AND <" form in ON and WHERE clauses. It will make your code bullet proof in any situation that I can think of. I try to not use BETWEEN even if columns are supposedly guaranteed to always be whole dates and even when the new DATE datatype is being used because future requirements and modifications can cause the datatype of a column to be changed.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy