根据客户一定时间段内的消费金额,更新客户的等级(A,B,C)当消费金额大于10万为A,5-10万为B,0-5万为C;
同时,消费金额取以下时间段内的消费金额,1、客户的发展之日 date1 2、今天日期 date2 3、去年的今天  date3
情况:当date3>=date1时,消费总金额取date3-date2期间的订单金额
      当date3<date1时,总金额取date1-date2期间内的订单金额表结构:客户表account(account_id,dengji,)、订单表contract(contract_id,account_id,start_time,money)
                      客户ID      客户等级                   订单ID      客户id     订单日期   订单金额
有哪位大哥帮忙写写啊,有不明白的地方尽管问小弟吧,3ku了
                                            

解决方案 »

  1.   

    start_time是什么意思.
    1.如果是客户的发展之日,没有订单发生金额的时间.
    2.如果是订单发生金额的时间,没有客户发展之日
      

  2.   

    不知道是不是好用,试试吧update a
    set dengji=
    case 
    when (select sum(case when dateadd(year,-1,getdate())>=start_time then 0 else money end) 
    from contract b where a.account_id=b.account_id)>100000 then 'A'
    when (select sum(case when dateadd(year,-1,getdate())>=start_time then 0 else money end) 
    from contract b where a.account_id=b.account_id)<=50000 then 'C'
    else 'B'
     end
    from account a 
      

  3.   


    create table #a(id int,dengji nvarchar(2))
    create table #b(bid int,aid int,Start_time datetime,money money)insert into #a
    select 1,'' union all
    select 2,'' union all
    select 3,''insert into #b
    select 1,1,'2009-07-24',50000 union all
    select 2,1,'2010-06-07',100000 union all
    select 3,2,'2010-05-08',80000 union all
    select 4,3,'2009-05-07',10000
    Update #a
    set dengji=case when xfje >=100000 then 'A' when xfje <100000 and xfje>=50000 then 'B'
    else 'C' end  from (
    --去年今天>=第一次消费日期 时,消费总金额取 去年今天-今天 期间的订单金额
    select aid,sum(money) xfje from #b 
    where aid in
    (
    --计算出 去年今天>=第一次消费日期 的数据,然后得出 客户ID
    select aid from #b
    where Start_time<=Dateadd(yy,-1,getdate())
    )
    and Start_time>=Dateadd(yy,-1,getdate()) and Start_time<=getdate()
    group by aid
    union all
    --去年今天<第一次消费日期时,总金额取 第一次消费日期-今天 期间内的订单金额
    select aid,sum(money) xfje from #b where aid not in(select aid from #b
    where Start_time<=Dateadd(yy,-1,getdate()))
    and Start_time<=getdate()
    group by aid) a where a.aid=#a.id
      

  4.   

    引用的这位大哥的,测试结果这个是对的!update #a
    set dengji= 
        case 
            when (select sum(case when dateadd(year,-1,getdate())>=start_time then 0 else money end) 
                    from #b b where #a.id=b.aid)>=100000 then 'A'
            when (select sum(case when dateadd(year,-1,getdate())>=start_time then 0 else money end) 
                    from #b b where #a.id=b.aid)<=50000 then 'C'
            else 'B'
         end
    from #a