Hi I have the following problem and I am trying to work out what is the best way to solve.
I have a table say called [Kpi] and the data would be like this:-
[ContractId] [KpiId] [NotificationIntervalInMonths]
1000 1 3
1000 2 5
I have a [Contract] table which contains:-
[ContractId] [StartDate] [EndDate]
1000 1/Nov/2009 4/Apr/2011
I am after a way to show a schedule of notifications for when the Kpi are due to notify a user between the start and end date of the contract e.g. The structure above would create the following columns/rows:-
[ContractId] [KpiId] [NotificationDate]
1000 1 1/Feb/2009
1000 1 1/May/2010
1000 1 1/Aug/2010
1000 1 1/Nov/2010
1000 1 1/Feb/2011
1000 2 1/Apr/2010
1000 2 1/Sep/2010
1000 2 1/Feb/2011
I first of all thought that I would create a lookup table that got populated a every time I inserted a new Kpi, this seems feasible and may seem the best approach.
My business logic dictates that the [NotificationIntervalInMonths] can't be changed, however the [EndDate] of a contract can change. This means that I would have to add/delete records in the lookup table based on the new contract [EndDate] and to me this seems a bit messy.
So this leads me to my question, is there a pure SQL approach that can get me a schedule of notifications without creating a look up table? Cursors are not allowed :P but I hoping CTE's would work here.
If I have not given enough information then please ask.