根据客户一定时间段内的消费金额,更新客户的等级(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、客户的发展之日 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了
解决方案 »
- 数据库里的数据分行分断
- ACCESS 中有两个数据表,如何把一个表的数据 拷贝到 另一个表 ??
- 再进来看看,我亲爱的兄弟姐妹门
- 如何查找第二大数据
- 再请问邹建和众高手,同时有SQLSERVER和MSDE(新建实例安装),那么连接MSDE中的数据库的字符串应该是什么?
- 急:c++ builder中 如何写 pro*c!!!
- 请教:如何将sql server直接打包入程序
- 新手请教:请问SQLserver7.0中DataTransformationServices有什么用,它的3个子模块怎么用?是不是跟事物处理有关?事物处理是个什么概念?
- MySQL与C语言的问题?
- sdl,update 中的减负数的问题
- SQL SERVER2005 分页函数
- SQL Server 2005的问题
1.如果是客户的发展之日,没有订单发生金额的时间.
2.如果是订单发生金额的时间,没有客户发展之日
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
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
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