ShankarJ
Programmer
- Aug 9, 2003
- 856
Hi!
I have data in the following format :
Table
-----
ID int
Rate decimal(9,2)
EffDate datetime
Data
----
1,200,2005-06-16
1,200,2005-06-17
1,300,2005-06-18
1,300,2005-06-20
1,200,2005-06-21
1,200,2005-06-22
I want to query which shows the Rate along with the validity dates i.e. MIN() & MAX() by Rate but taking into account Rate changes:
1, 200, 2005-06-16, 2005-06-17
1, 300, 2005-06-18, 2005-06-18
1, 300, 2005-06-20, 2005-06-20
1, 200, 2005-06-21, 2005-06-22
Please Note:
The Rate has changed on 2005-06-18 and again on 2005-06-21
There is no rate for 2005-06-19
My query of
SELECT ID, Rate, MIN(EffDate), MAX(EffDate), COUNT(*) AS Days GROUP BY ID, Rate
returns the following:
1, 200, 2005-06-16, 2005-06-22, 4
1, 300, 2005-06-18, 2005-06-20, 2
Any suggestion on how to do the query to make the GROUP BY break on a Rate Change or a missing EffDate. Or, is the only option a Stored procedure using a Temp Table? If so, how?
Thanks & Regards
I have data in the following format :
Table
-----
ID int
Rate decimal(9,2)
EffDate datetime
Data
----
1,200,2005-06-16
1,200,2005-06-17
1,300,2005-06-18
1,300,2005-06-20
1,200,2005-06-21
1,200,2005-06-22
I want to query which shows the Rate along with the validity dates i.e. MIN() & MAX() by Rate but taking into account Rate changes:
1, 200, 2005-06-16, 2005-06-17
1, 300, 2005-06-18, 2005-06-18
1, 300, 2005-06-20, 2005-06-20
1, 200, 2005-06-21, 2005-06-22
Please Note:
The Rate has changed on 2005-06-18 and again on 2005-06-21
There is no rate for 2005-06-19
My query of
SELECT ID, Rate, MIN(EffDate), MAX(EffDate), COUNT(*) AS Days GROUP BY ID, Rate
returns the following:
1, 200, 2005-06-16, 2005-06-22, 4
1, 300, 2005-06-18, 2005-06-20, 2
Any suggestion on how to do the query to make the GROUP BY break on a Rate Change or a missing EffDate. Or, is the only option a Stored procedure using a Temp Table? If so, how?
Thanks & Regards