Is there w way to achieve following using an SQL 2000 query, i looked everywhere but could not find any working snippet.
I have contiguous date segments and requirement is to get the min effective date and maximum effective dates for each contiguous dates.
if that is not possible getting min effective date and max termdate for an contiguous segment using different queries will also work for me.
ID effdate termdate
1 2007-05-01 2007-05-31
2 2007-06-01 2007-06-30
3 2007-07-01 2007-09-30
4 2008-03-01 2008-03-31
5 2008-05-01 2008-05-31
6 2008-06-01 2008-06-30
Expected Result :
2007-05-01 2007-09-30
2008-03-01 2008-03-31
2008-05-01 2008-06-30
I don't think this is possible without the use of a temporary table. (You don't exclude their use, but you didn't exclude using cursors either until Mike Bennett had posted his answer)
I'm reasonably confident that this is a generic solution - it uses an undocumented feature where it's possible to change the value of a variable more than once during an update statement.
You may be able to skip the creation of an artificial identity column to guarantee order (autoID in my query) if the records in your table are entered in order of effdate.
I did something like this to get the effdate and same for termdate, made them as two separate views and got the final result.
Unfortunately you're probably going to have to use a cursor. Something like this should work.
select min(effdate),max(termdate) from tab1 where year(effdate) = year(termdate) group by year(effdate) order by min(effdate)
Result
2007-05-01 00:00:00.000 2007-09-30 00:00:00.000
2008-03-01 00:00:00.000 2008-06-30 00:00:00.000