(一个商品价格表)
商品编码  商品名称    价格  上市日期
  1         钢笔        5    2010-08-18      (折扣表)
商品编码    天数     折扣
   1         30      0.8
   1         60      0.6想要得到的结果:商品上市1个月后,商品价格表里的5自动更新为5*0.8,也就是4,
                商品上市2个月后,商品价格表里的5自动更新为5*0.6,也就是3,    问一下该怎么做,是要做一个作业吗?怎么写? 现在一点思路也没有。

解决方案 »

  1.   

    新建一个作业  用以下的语句
    declare @tA table([商品编码] int,[商品名称] varchar(10),[价格] decimal(10,2),[上市日期] varchar(12))
    declare @tB table([商品编码] int,[天数] int,[折扣] decimal(10,2))
    insert into @ta ([商品编码],[商品名称],[价格],[上市日期]) values(1,'钢笔' , 5,'2010-05-18')
    insert into @tb([商品编码],[天数],[折扣]) values (1,30,0.8)
    insert into @tb([商品编码],[天数],[折扣]) values (1,60,0.6)select * From @taupdate @ta set [价格]=a.[价格]*b.[折扣] from @ta a,
    (select a.[商品编码],b.[折扣] from @ta a,@tb b ,
    (select a.[商品编码],max([天数]) as [天数] from @ta a ,@tb b where a.[商品编码]=b.[商品编码]
    and datediff(day,a.[上市日期],getdate() )>=b.[天数] group by a.[商品编码]) c
    where a.[商品编码]=b.[商品编码] and b.[商品编码]=c.[商品编码] and b.[天数]=c.[天数] )
    b where a.[商品编码]=b.[商品编码]select * From @ta
    不过我觉得你应该还需要一个字段,记录原始的价格
      

  2.   

    新建一个作业,每天早上00:00:00 让它跑一次就行了。--测试数据
    select * into #temp1 from (
    select 1 as 商品编码,'钢笔' as 商品名称,5 as 价格,'2010-08-18' as 上市日期 union all
    select 2 as 商品编码,'铅笔' as 商品名称,1 as 价格,'2010-08-18' as 上市日期) a
     select * into #temp2 from (
    select 1 as 商品编码,30 as 天数 ,0.8 as 折扣 union all
    select 1 as 商品编码,60 as 天数 ,0.6 as 折扣 union all
    select 2 as 商品编码,30 as 天数 ,0.8 as 折扣 union all
    select 2 as 商品编码,60 as 天数 ,0.6 as 折扣  ) a
    ----
    create procedure P_AutoUpdatePrice
    --自动更新价格SP,做个作业,第天00:00:00时运行一次,就OK了。
    as
    update #temp1 set [价格]=[价格]*b.[折扣] from #temp1 a,#temp2 b where a.[商品编码]=b.[商品编码] and datediff(day,a.[上市日期],getdate())=b.天数GO
      

  3.   

    改一下上面的SP,这样可能更好create procedure P_AutoUpdatePrice
    --自动更新价格SP,做个作业,第天00:00:00时运行一次,就OK了。
    as
    update #temp1 set [价格]=cast(cast([价格] as float)*b.[折扣] as float) from #temp1 a,#temp2 b where a.[商品编码]=b.[商品编码] and datediff(day,a.[上市日期],getdate())=b.天数GO
      

  4.   

    create procedure P_AutoUpdatePrice
    --自动更新价格SP,做个作业,第天00:00:00时运行一次,就OK了。
    as
    update #temp1 set [价格]=cast(cast([价格] as float)*b.[折扣] as float) from #temp1 a,#temp2 b where a.[商品编码]=b.[商品编码] and datediff(day,a.[上市日期],getdate())=b.天数GO