| Author |
Topic |
|
parody
Posting Yak Master
111 Posts |
Posted - 2011-12-05 : 07:09:31
|
| HiI am investigating various methods and performance of this, and wonder if anyone can suggest and nuggets of wisdom I havent yet considered.So for a table thus:Key Data FromDate ToDate1 A 20111201 201101011 B 20111202 NULL2 A 20111205 NULL3 A 20111201 201112023 B 20111203 201112043 C 20111205 NULLThe result, from a query that can be used in the definition of a view, should be:Key Data FromDate ToDate Date1 A 20111201 20111201 201112011 B 20111202 NULL 201112021 B 20111202 NULL 201112031 B 20111202 NULL 201112041 B 20111202 NULL 201112052 A 20111201 NULL 201112012 A 20111201 NULL 201112022 A 20111201 NULL 201112032 A 20111201 NULL 201112042 A 20111201 NULL 201112053 A 20111201 20111202 201112013 A 20111201 20111202 201112023 B 20111203 20111204 201112033 B 20111203 20111204 201112043 C 20111205 NULL 20111205So, create a row for every date between each rows from and to dates. There is the possibility of some preprocessing if it is worthwhile. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-05 : 07:16:06
|
| [code]SELECT t.*,DATEADD(dd,v.number,FromDate) AS [Date]FROM yourtable tCROSS JOIN master..spt_values vWHERE v.type='p'AND (DATEADD(dd,v.number,FromDate) BETWEEN FromDate AND ToDateOR (DATEADD(dd,v.number,FromDate) BETWEEN FromDate AND GETDATE() AND ToDate IS NULL))[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
parody
Posting Yak Master
111 Posts |
Posted - 2011-12-05 : 07:26:03
|
OK, similar to one I had:FROM mytable mtINNER JOIN mylookup lkON lk.Date BETWEEN mt.FromDate AND isnull(mt.EndDate,convert(date,getdate()) but will look at the dateadd option thanks. Any more?! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-05 : 07:30:56
|
quote: Originally posted by parody OK, similar to one I had:FROM mytable mtINNER JOIN mylookup lkON lk.Date BETWEEN mt.FromDate AND isnull(mt.EndDate,convert(date,getdate()) but will look at the dateadd option thanks. Any more?!
what does that mean?isnt given suggestion enough for you? ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
parody
Posting Yak Master
111 Posts |
Posted - 2011-12-05 : 07:32:50
|
| It's great, thanks.I just meant does anyone else have any alternatives, more than one way to skin a cat and all that... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-05 : 07:42:58
|
| you can use CTE method also if you're on sql 2005 or above------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
parody
Posting Yak Master
111 Posts |
Posted - 2011-12-05 : 07:46:40
|
| Unfortunately the dateadd method does not have favourable performance compared to the existing methods.Yes I used a CTE to start with for lookup, but decided to store in permanent table as it is static and doesnt need to be recalculated on every execution. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-12-05 : 11:35:31
|
quote: Originally posted by parody Unfortunately the dateadd method does not have favourable performance compared to the existing methods.Yes I used a CTE to start with for lookup, but decided to store in permanent table as it is static and doesnt need to be recalculated on every execution.
can you tell which existing methods you're comparing it to?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|