可能是这样的:
-- test tables and test data
declare @t table(Product char(8), DiscountStart char(6), DiscountEnd char(6), Rate char(3))
insert @t values
('Product1','Dec/10','Jan/05','10%'),
('Product2','Nov/01','Dec/31','12%'),
('Product3','May/01','Jun/30','08%'),
('Product4','Sep/01','Sep/15','05%')
declare @productlist table(selldate datetime, product char(8))
insert @productlist values('20130101', 'Product1')
insert @productlist values('20130501', 'Product2')
insert @productlist values('20151231', 'Product1')
select pl.selldate, pl.product, coalesce(x.rate, '00%') rate from
@productlist pl
outer apply
(
select rate, a,b from
(
select rate, convert(datetime, cast(year(pl.selldate) as char(4))+'-' + DiscountStart, 106) a,
case when convert(datetime, cast(year(pl.selldate) as char(4))+'-' + DiscountEnd, 106) < convert(datetime, cast(year(pl.selldate) as char(4))+'-' + DiscountStart, 106)
then convert(datetime, cast(year(pl.selldate) + 1 as char(4))+'-' + DiscountEnd, 106) else convert(datetime, cast(year(pl.selldate) as char(4))+'-' + DiscountEnd, 106) end b
from @t
where pl.Product = Product
union all
select rate, convert(datetime, cast(year(pl.selldate)-1 as char(4))+'-' + DiscountStart, 106) a,
case when convert(datetime, cast(year(pl.selldate)-1 as char(4))+'-' + DiscountEnd, 106) < convert(datetime, cast(year(pl.selldate)-1 as char(4))+'-' + DiscountStart, 106)
then convert(datetime, cast(year(pl.selldate) + 1-1 as char(4))+'-' + DiscountEnd, 106) else convert(datetime, cast(year(pl.selldate)-1 as char(4))+'-' + DiscountEnd, 106) end b
from @t
where pl.Product = Product
) x1
WHERE pl.selldate between x1.a and x1.b
) x