1

表有列 DiscountStart 和 DiscountEnd。两列都包含 Month+'/'+Day 值。

 Product     DiscountStart     DiscountEnd     Rate
 --------------------------------------------------
 Product1     'Dec/10'          'Jan/05'        10% 
 Product2     'Nov/01           'Dec/31'        12% 
 Product3     'May/01'          'Jun/30'        08% 
 Product4     'Sep/01'          'Sep/15'        05%

DiscountStart如果购买日期介于购买年份和购买年份之间,我需要为产品应用折扣率DiscountEnd。请帮忙。

4

3 回答 3

0

要获得给定日期的适用折扣,

select
*
from
(
    select 
        product,
        startdate,
        case when (enddate<startdate) then DATEADD(YY,1,enddate)else enddate end enddate,
        rate
    from
        (
            select 
                CONVERT(date, convert(varchar(4),year(getdate()))+'/'+discountstart) as startdate,
                CONVERT(date, convert(varchar(4),year(getdate()))+'/'+discountend) as enddate,
                product, rate from yourtable
            union 
            select 
                CONVERT(date, convert(varchar(4),year(getdate())-1)+'/'+discountstart) as startdate,
                CONVERT(date, convert(varchar(4),year(getdate())-1)+'/'+discountend) as enddate,
                product, rate from yourtable

        ) v
) discounts
where GETDATE() between startdate and enddate

从那里,您可以left join对您的购买进行简单的操作,以查看是否适用任何折扣。

于 2013-10-02T08:22:50.910 回答
0

可能是这样的:

-- 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
于 2013-10-02T10:55:25.340 回答
0

不知道为什么要这样存储日期范围,但我建议 WHERE <PurchaseDate> BETWEEN DiscountStart and DiscountEnd一般使用。我会解析出 3 个字母的代码并将其替换为数字,然后添加年份。像这样:

SELECT p.Price * DiscTbl.Rate DiscountedRate, *
FROM 
(
    SELECT
        Product, 
        CASE LEFT(DiscountStart, 3) 
          WHEN 'JAN' THEN '01' 
          WHEN 'FEB' THEN '02'
          WHEN 'MAR' THEN '03'
          .
          .
          .
          ELSE NULL END 
          + RIGHT(DiscountStart, 3) + '/'
          + CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4)) AS StartDate
       , 
       CASE LEFT(DiscountEnd, 3) 
          WHEN 'JAN' THEN '01' 
          WHEN 'FEB' THEN '02'
          WHEN 'MAR' THEN '03'
          .
          .
          .
          ELSE NULL END 
          + RIGHT(DiscountEnd, 3) + '/'
          + CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4)) AS EndDate,
       Rate
    FROM DiscountTable --Named for the sake of this example
) DiscTbl
join Purchases p --Named for the sake of this example
     on p.Product = DiscTbl.Product 
    and p.PurchaseDate BETWEEN 
                          CASE WHEN DiscTbl.StartDate > DiscTbl.EndDate 
                               THEN DATEADD(YEAR, -1, DiscTbl.StartDate)
                               ELSE DiscTbl.StartDate 
                          END
                       AND DiscTbl.EndDate 
于 2013-10-02T00:10:02.597 回答