你这说的是你们公司业务。听不懂(大家都忙,没有时间去理解你公司这一堆复杂的业务)。。。。你说一下你的数据是如何的。。现在要根据这个数据如何计算。。把你现在有的数据截图出来。再说一下这个数据最终要什么结果。。以及说一下为什么结果是这么多
select
t1.yewu,--业务员
t1.slae_total,--任务
t2.slae,
t2.order_id,
t2.cost_name,
sum(t2.slae) over(PARTITION BY t2.yewu ORDER BY t2.yewu,t2.date_name asc) as 累计,
t1.slae_total-sum(t2.slae) over(PARTITION BY t2.yewu ORDER BY t2.yewu,t2.date_name asc) as 销量完成金额,
/*判断后的金额用于计算*/
case when
t1.slae_total-sum(t2.slae) over(PARTITION BY t2.yewu ORDER BY t2.yewu,t2.date_name asc) <0 then
(t1.slae_total-sum(t2.slae) over(PARTITION BY t2.yewu ORDER BY t2.yewu,t2.date_name asc))*-1 else 0 end as 判断后金额,
--ABCD客户级别的提成比例分别是4%、5%、6%、7%
case when t2.cost_name='A' then 0.04
when t2.cost_name='B' then 0.05
when t2.cost_name='C' then 0.06
when t2.cost_name='D' then 0.07
else 0.00 end as 比例
from
(select '张三' as yewu,25000 as slae_total
union all
select '李四' as yewu,50000 as slae_total
) t1 left join
(
select '李四' as yewu,1 as order_id,'A' as cost_name,20000 as slae,'2022-03-01' as date_name union all
select '李四' as yewu,4 as order_id,'A' as cost_name,10000 as slae,'2022-03-03' as date_name union all
select '李四' as yewu,3 as order_id,'B' as cost_name,15000 as slae,'2022-03-02' as date_name union all
select '李四' as yewu,8 as order_id,'B' as cost_name,10000 as slae,'2022-03-08' as date_name union all
select '李四' as yewu,9 as order_id,'C' as cost_name,10000 as slae,'2022-03-09' as date_name union all
select '李四' as yewu,12 as order_id,'D' as cost_name,10000 as slae,'2022-03-12' as date_name union all
select '张三' as yewu,2 as order_id,'A' as cost_name,7000 as slae,'2022-03-01' as date_name union all
select '张三' as yewu,6 as order_id,'A' as cost_name,6500 as slae,'2022-03-04' as date_name union all
select '张三' as yewu,5 as order_id,'B' as cost_name,3000 as slae,'2022-03-03' as date_name union all
select '张三' as yewu,7 as order_id,'B' as cost_name,10000 as slae,'2022-03-06' as date_name union all
select '张三' as yewu,11 as order_id,'C' as cost_name,5600 as slae,'2022-03-11' as date_name union all
select '张三' as yewu,10 as order_id,'D' as cost_name,10000 as slae,'2022-03-10' as date_name
) t2 on t1.yewu=t2.yewu