Given the following table and data.
create table prices
(productKey int
,PriceType char(10)
,BeginDate date
,EndDate date
,price decimal(18,2))
insert into prices(productKey, PriceType,BeginDate,EndDate, price)
values
(1,'LIST','1-1-2010','1-15-2010',10),
(1,'LIST','1-16-2010','10-15-2010',20),
(1,'DISCOUNT','1-10-2010','1-15-2010',-5),
(2,'LIST','2-1-2010','10-15-2010',30),
(2,'LIST','10-16-2010','1-1-9999',35),
(2,'DISCOUNT','2-10-2010','10-25-2010',-10),
(2,'LIST','1-1-2010','1-15-2010',10),
(3,'DISCOUNT','1-12-2010','1-1-9999',-5),
(3,'LIST','1-16-2010','1-1-9999',10)
I need to insert records into that same table that calculates the actual price (list-discount) for each time period.
e.g. for product 1, I should have the following “ACTUAL” records
Begin End Price
1-1-2010 1-9-2010 10
1-10-2010 1-15-2010 5
1-16-2010 10-15-2010 20
I kind of have it figured out for anything where a discount starts within a list price span, but I’m at a loss for anything else.
Thanks for the help
EDIT
There can be multiple discounts per ProductKey, but the discount periods won’t overlap. So you could have one for 2010, and another one for 2012, but not 2 for 2010.
Also, if someone can come up with a better title, please do so. My poor brain is completely challenged at this point.
EDIT2
It’s SQL server 2008R2. I’d love a beautiful set based answer (or someone that gives me a start in that direction), but will be just as happy with a cursor solution that works.
Clever puzzle.
You need to reconstruct all the time spans. To do this, I take out all the dates from the price ranges and reconstruct the possible date ranges.
I’ve thought about this some more. The basic idea above is correct. The basic idea is to break up the time dimension into intervals where the list and discount is constant over the entire interval. The question is how to create these intervals, which are in the datepairs alias.
These intervals have just a few rules:
Once we have the intervals, it is a simple matter to join in the appropriate list price and discounts for that period. The following query uses this logic: