求sql server语句算最终提成!救大命!酸Q!!!  或者有什么好的逻辑思路,先放个膝盖

示例.zip

Snipaste_2022-03-29_09-46-35.png1、月销售任务量按工资每500元按2500㎡算,超出25000㎡的销售量才算提成。那么如张三月薪5000,则月任务是25000㎡,李四月薪10000,则月任务是50000㎡;

2、每天有多条出货单据产生,有客户、销售量、单价、业务员....

3、那这任务量25000㎡按提成比例(即客户级别=A~E级别)先取够25000㎡,——如先取A客户、再取B客户、再取C客户......以此类推,如果到C客户的一半够了,就取靠前的销售日期下的部门C客户单,直到取够25000㎡,剩下的再按对应比例算提成

FineReport 牛气冲天的哇 发布于 2022-3-29 08:27 (编辑于 2022-3-29 10:24)
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共4回答
最佳回答
1
CD20160914Lv8专家互助
发布于2022-3-29 08:48(编辑于 2022-3-29 13:26)

你这说的是你们公司业务。听不懂(大家都忙,没有时间去理解你公司这一堆复杂的业务)。。。。你说一下你的数据是如何的。。现在要根据这个数据如何计算。。把你现在有的数据截图出来。再说一下这个数据最终要什么结果。。以及说一下为什么结果是这么多

image.png

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

  • 牛气冲天的哇 牛气冲天的哇(提问者) 哈哈 抱歉抱歉 谢指导 俺马上截出来
    2022-03-29 09:25 
  • CD20160914 CD20160914 回复 牛气冲天的哇(提问者) 为什么李四的订单9 10不算提成了??看不懂。为什么订单只算5000了?
    2022-03-29 09:32 
  • 牛气冲天的哇 牛气冲天的哇(提问者) 回复 CD20160914 沤 对不起您!!妹改备注... /(ㄒoㄒ)/~~重新上传了
    2022-03-29 09:53 
  • CD20160914 CD20160914 回复 牛气冲天的哇(提问者) 什么数据库。还有算提成的时候。。是如何计算提成 的。。
    2022-03-29 09:53 
  • 牛气冲天的哇 牛气冲天的哇(提问者) 回复 CD20160914 sql server, 算提成就,ABCD客户级别的提成比例分别是4%、5%、6%、7%,提成就按销售量*单价(算1元)*提成比例
    2022-03-29 10:12 
最佳回答
1
free_zzLv6中级互助
发布于2022-3-29 10:19(编辑于 2022-3-29 11:03)

你是想用sql算出什么

通过月销售任务量来算啥

我看你这个月销售任务量必须要先是已知的条件,通过月销售任务量去算工资吗,或者算提成?

----------------------------------------------------------------------------------------------

with tab1 as (select 

a.业务员

,b.客户

,a.月基本销售额 - (sum(b.销售量) over(PARTITION BY b.业务员  (ORDER BY b.客户,b.销售日期 asc))) as 提成销售量

from table1 a left join table2 b on a.业务员 = b.业务员)

select 

业务员,

sum(case when 客户 = 'A' then 提成销售量 * 单价(算1元)* 4%  

        when 客户 = 'B' then 提成销售量 * 单价(算1元)* 5% 

        when 客户 = 'C' then 提成销售量 * 单价(算1元)* 6% 

        when 客户 = 'D' then 提成销售量 * 单价(算1元)* 7% 

         else 0 end) as 提成

 from tab1

group by 业务员

  • 牛气冲天的哇 牛气冲天的哇(提问者) 您好!最终是算业务员能拿多少提成,提成是销售量*单价*提成比例,前提是扣去月销售任务量对应的单
    2022-03-29 10:27 
  • free_zz free_zz 回复 牛气冲天的哇(提问者) 试试
    2022-03-29 10:58 
  • 牛气冲天的哇 牛气冲天的哇(提问者) 回复 free_zz 谢谢您!我就去试试!(p≧w≦q)
    2022-03-29 13:50 
最佳回答
0
Z4u3z1Lv6专家互助
发布于2022-3-29 08:54

赞同楼上说法,另外求SQL也得把你用的什么库也报出来啊,数据库不同,函数什么的都有差异..........

最佳回答
0
ScyalcireLv7中级互助
发布于2022-3-29 08:55

好家伙  出数学题呢

  • 5关注人数
  • 744浏览人数
  • 最后回答于:2022-3-29 13:26
    请选择关闭问题的原因
    确定 取消
    返回顶部