日期:2014-05-18  浏览次数:20492 次

请假复杂的sql语句
关于业务员提成的问题:    
1、在07年3月之前登记的客户无论金额都属于老客户。07年3月1日开始登记的客户如果在07年3月之前已经登记过仍归于老客户。        
2、07年3月1日开始登记的客户在07年3月之前从未登记过归类为新客户,新客户按两种        
方式可归入老客户:    
1、按时间:07年3月1日开始登记的客户从第一次登记日起算到半        
年的时间无论金额大小一律归于老客户    
2、按发生金额算:07年3月1日开始登记的客        
户从第一次登记日起算到半年发生金额达到五千元后归于老客户。        
      新客户提成6%,老客户提成4%    
 
 
两张表:    
客户表:   Customer  
客户编号(C_ID)     名字(C_Name)             添加时间(C_time)    
                 
合同表:     Hetong
客户编号(T_ID)       合同编号(ID)     添加时间(time)     金额(money)    
 
 
要求查出来的结果为    
 
合同编号(ID)   添加时间(time)   金额(money)     提成率(percent)     提成额(Tmoney)    
 
提成率根据上面的要求得出。提成额就是金额和提成率的相乘。

------解决方案--------------------
有点晕,不大懂
------解决方案--------------------
milair answer

HTTP://www.ebigear.com/Fund/PlayNews.php?NewsID=28982&ID=285885
------解决方案--------------------
客户表 Customer 里面的客户编号(C_ID) 是不是不重复?
如果一个客户登陆两次 ,在Customer 有几条记录?添加时间(C_time) 是那次的?
合同表 Hetong 是不应该有两条关于这个人的记录。
今天下午就放假了,帮你想想,一个SQL可能写不出来,存储过程可以嘛?
------解决方案--------------------
select
h.id as 合同编号,
h.time as 添加时间,
h.[money] as 金额,
case when c.time < '2007-3-1 ' then 0.04
when h.time> dateadd(month,6,c.time) then 0.04
when (select sum([money]) from Hetong where T_ID=c.C_ID and time> = '2007-3-1 ' and time <h.time)> 5000 then 0.04
else 0.06
end as 提成率,
h.[money] *
case when c.time < '2007-3-1 ' then 0.04
when h.time> dateadd(month,6,c.time) then 0.04
when (select sum([money]) from Hetong where T_ID=c.C_ID and time> = '2007-3-1 ' and time <h.time)> 5000 then 0.04
else 0.06
end as 提成额
from
Customer c,
Hetong h
where
c.C_ID=h.T_ID
order by
c.C_ID,
h.time