【问题标题】:Calculate a Slide rate commission per agent with MySQL... AND in ON statement?使用 MySQL 计算每个代理的滑动费率佣金...... AND 在 ON 语句中?
【发布时间】:2017-01-13 11:13:33
【问题描述】:

我已经为这个问题设置了一个 sqlfiddle:see here

我在根据代理商的销售额(此处称为 NGR)为代理商计算滑动费率佣金时遇到问题。

佣金表如下:

+----+------------+----------+----------+----------+--------+
| id |  tier_min  | tier_max | com_rate | dif_rate | agent  |
+----+------------+----------+----------+----------+--------+
|  1 |          0 |   100000 |     0.15 |     0.15 | AGENT4 |
|  3 | 100000.001 |   200000 |      0.2 |     0.05 | AGENT4 |
|  4 | 200000.001 |   300000 |     0.25 |     0.05 | AGENT4 |
|  5 | 300000.001 |   500000 |      0.3 |     0.05 | AGENT4 |
|  6 | 500000.001 | 10000000 |     0.35 |     0.05 | AGENT4 |
+----+------------+----------+----------+----------+--------+

例如,如果一个Agent在一个时期有120000美元的销售额,他从0到100 000(15000)获得15%,从阈值100000到200000(20000*20%=4000)获得20%,总计19000

mysqlfiddle中详述的mysql查询详细说明了工作公式:

sum((s.NGR - r.tier_min) * r.dif_rate) as SlideCom

最终查询如下:

+--------+---------+------------+-------+----------+----------+
| Agent  | APmonth | FirstMonth |  NGR  | FIXEDCOM | VARCOM2  |
+--------+---------+------------+-------+----------+----------+
| AGENT1 |       3 |          0 | 16802 |        0 | 2520.327 |
| AGENT2 |      17 |          2 |  7926 |       60 | 1188.975 |
| AGENT3 |       3 |          0 |  6541 |        0 |   981.15 |
| AGENT4 |       4 |          0 |  5513 |        0 |   826.95 |
| AGENT5 |       1 |          0 |     3 |        0 |      0.6 |
| AGENT6 |       1 |          0 |     1 |        0 |     0.21 |
+--------+---------+------------+-------+----------+----------+

我的问题:

  • 我无法解决我需要每个代理具有其特定浮动佣金比例的问题,并将佣金表中的代理字段与其对应的代理销售 (NGR) 相关联。我尝试修改添加到的查询

在 r.tier_min

但它不起作用。 在这个问题上的任何帮助都会有很大的帮助,因为我一直在尝试用我有限的 Mysql 技能自己做这件事;)

谢谢!

关于 sql fiddle 问题的完整数据:

  CREATE TABLE commissions
(`id` int, `tier_min` double, `tier_max` double, `com_rate` double, `dif_rate` double, `agent` varchar(50))
;

INSERT INTO commissions
(`id`, `tier_min`, `tier_max`, `com_rate`, `dif_rate`, `agent`)
VALUES
(1, 0, 100000, .15, .15, 'AGENT4'),
(3, 100000.001, 200000, .2, .05, 'AGENT4'),
(4, 200000.001, 300000, .25, .05, 'AGENT4'),
(5, 300000.001, 500000, .3, .05, 'AGENT4'),
(6, 500000.001, 10000000, .35, .05, 'AGENT4')
;


CREATE TABLE Online_customer_activity_v2
(`id` int, `Date` datetime, `Customers` varchar(100), `Agent` varchar(100), `Real_Money` int, `_Bonuses` int, `Total_Bets` int,  `Total_Win_Loss` int)
;

INSERT INTO Online_customer_activity_v2
(`id`, `Date`, `Customers`, `Agent`, `Real_Money`, `_Bonuses`, `Total_Bets`, `Total_Win_Loss`)
Values
(6813, '2017-01-01 00:00:00', 'KingOfPop', 'AGENT2',101000,1000,4000,100500),
(6814, '2017-01-01 00:00:00', 'Serena77', 'AGENT4',130000,1000,514500,120000),
(6815, '2017-01-01 00:00:00', 'KerymNY', 'AGENT3',376,0,1267.65,375.93),
(6816, '2017-01-01 00:00:00', 'whatthelol', 'AGENT1',130,0,6233.5,119.4993),
(6817, '2017-01-01 00:00:00', 'Noukashi', 'AGENT2',0,0,343.4,49.8),
(6818, '2017-01-01 00:00:00', 'SeaSunBeach', 'AGENT2',30,0,654.5,30),
(6819, '2017-01-01 00:00:00', 'Rizo_090', 'AGENT3',400,0,2675,-1165),
(6820, '2017-01-02 00:00:00', 'Rizo_090', 'AGENT3',7900,1200,140168,30199)
;

CREATE TABLE Online_playerdatabase_v2
(`id` int,  `Player` varchar(50), `Agent` varchar(50),  `Sign_Up_Date` varchar(12),  `First_Deposit_Date` varchar(18))
;

INSERT INTO Online_playerdatabase_v2
(`id`, `Player`, `Agent`, `Sign_Up_Date`, `First_Deposit_Date`)
VALUES
(75, 'KerymNY', 'AGENT3', '2015-02-21', '2015-02-26'),
(137, 'Rizo_090', 'AGENT3', '2015-10-23', '2015-10-23'),
(286, 'KingOfPop', 'AGENT2', '2016-10-03', '2016-12-21'),
(6, 'Noukashi', 'AGENT2', '2016-07-21', '2016-07-22'),
(294, 'Serena77', 'AGENT4', '2016-10-09', '2017-01-02'),
(160, 'whatthelol', 'AGENT1', '2015-03-01', '2015-03-05'),
(360, 'SeaSunBeach', 'AGENT2', '2016-04-18', '2016-04-18')
;
SELECT
    a.Agent ,
    t2.APmonth ,
    COALESCE(t8.FirstMonth , 0) AS FirstMonth ,

    TRUNCATE(a.NGR , 0) AS NGR ,


    COALESCE(t8.FirstMonth , 0) * 30 AS FIXEDCOM ,

    COALESCE(yy.SlideCom , 0) AS VARCOM2 ,
    TRUNCATE(
        COALESCE(t8.FirstMonth , 0) * 30 + COALESCE(yy.SlideCom , 0) ,
        0
    ) AS totalCOM
FROM
    (
        SELECT
            Online_customer_activity_v2.Agent ,
            sum(
                Online_customer_activity_v2._Bonuses
            ) AS BONUS ,
            sum(
                Online_customer_activity_v2.Total_Win_Loss
            ) - sum(
                Online_customer_activity_v2._Bonuses
            ) AS NGR 
        FROM
            Online_customer_activity_v2
        WHERE
            (
                Online_customer_activity_v2.Date BETWEEN '2017-01-01'
                AND '2017-01-12'
                AND Agent <> 'NOAGENT'
            )
        GROUP BY
            Online_customer_activity_v2.Agent
    ) a
LEFT JOIN(
    SELECT
        Agent ,
        count(First_Deposit_Date) AS FirstMonth
    FROM
        Online_playerdatabase_v2
    WHERE
        Online_playerdatabase_v2.First_Deposit_Date BETWEEN '2017-01-01'
    AND '2017-01-12'
    GROUP BY
        Agent
) t8 ON a.Agent = t8.Agent
LEFT JOIN(
    SELECT
        Online_customer_activity_v2.Agent ,
        COUNT(DISTINCT(Customers)) AS APmonth
    FROM
        Online_customer_activity_v2
    WHERE
        (
            Online_customer_activity_v2.Date BETWEEN '2017-01-01'
            AND '2017-01-12'
            AND Agent <> 'NOAGENT'
        )
    AND Online_customer_activity_v2.Total_Bets > 0
    GROUP BY
        Agent
) AS t2 ON a.Agent = t2.Agent

LEFT JOIN(
    SELECT
        s.Agent ,
        sum((s.NGR - r.tier_min) * r.dif_rate) SlideCom
    FROM
        (
            SELECT
                Date ,
                Online_customer_activity_v2.Agent ,
                sum(
                    Online_customer_activity_v2.Total_Win_Loss
                ) - sum(
                    Online_customer_activity_v2._Bonuses
                ) AS NGR
            FROM
                Online_customer_activity_v2
            WHERE


                    Agent <> 'NOAGENT'

            AND (Date BETWEEN '2017-01-01'
            AND '2017-01-12')

            GROUP BY
                Agent ,
                date_format(Date , '%Y-%m')
        ) s
    JOIN commissions r ON r.tier_min <= s.NGR
    GROUP BY
        Agent
) yy ON a.Agent = yy.Agent
ORDER BY
    NGR DESC;

【问题讨论】:

  • 佣金率是固定的还是每个代理的?
  • 您好,佣金是每个代理的,所以每个代理可以有不同的比例(例如:AGENT1:从 0 到 100 000 的 10%,AGENT2:从 0 到 100 000 的 15%)

标签: mysql


【解决方案1】:

我想这就是你要找的:

SeLeCt t5.Agent,t6.APmonth,t6.FirstMonth,t5.NGR,t5.toRate,t5.comm  FrOm
(
    SELECT t2.Agent,t2.NGR,ROUND(sum(t2.toRate),2) AS 'toRate',ROUND(sum(t2.comm),2) AS 'comm' FROM
    (
        select t1.Agent,t1.NGR,if(t1.NGR-c.tier_min>0,
                                        if(t1.NGR>c.tier_max,c.tier_max-c.tier_min,t1.NGR-c.tier_min),
                                                            0) as 'toRate', 
                if(t1.NGR-c.tier_min>0,
                                        if(t1.NGR>c.tier_max,c.tier_max-c.tier_min,t1.NGR-c.tier_min),
                                                            0)*c.com_rate as 'comm'
        from myDB.commissions c ,
        (
        SELECT ocav2.Agent, sum(ocav2.Total_Win_Loss - ocav2._Bonuses) as 'NGR'
        FROM myDB.Online_customer_activity_v2 ocav2
        GROUP BY ocav2.Agent
        ) t1
        where c.agent = t1.Agent
    ) t2
    GROUP BY t2.Agent

) t5 LeFt JoIn

(
    select t3.Agent,t3.APmonth,if(t4.FirstMonth is null,0,t4.FirstMonth) as 'FirstMonth' from
    (
        SELECT oca.Agent,COUNT(DISTINCT(oca.Customers)) AS 'APmonth'
        FROM Online_customer_activity_v2 oca
        WHERE oca.Total_Bets>0 AND oca.Date BETWEEN '2017-01-01' AND '2017-01-12'
        GROUP BY oca.Agent

    ) t3 LEFT JOIN 
    (
    SELECT
        opv2.Agent ,
        count(opv2.First_Deposit_Date) AS 'FirstMonth'
    FROM
        Online_playerdatabase_v2 opv2
    WHERE
        opv2.First_Deposit_Date BETWEEN '2017-01-01' AND '2017-01-12'
    GROUP BY
        opv2.Agent
    ) t4
    on t3.Agent=t4.Agent
) t6 On t5.Agent=t6.Agent
;

记得在你的数据库中更改“myDB

【讨论】:

    【解决方案2】:

    给定

    drop table if exists t;
    create table t( id int,  tier_min  int, tier_max int, com_rate decimal(10,2), dif_rate decimal(10,2), agent varchar(10));
    truncate table t;
    insert into t values
    (  1 ,          0 ,   100000,     0.15 ,     0.15 , 'AGENT4'),
    (  3 , 100000.001 ,   200000 ,      0.2 ,     0.05 , 'AGENT4'),
    (  4 , 200000.001 ,   300000 ,     0.25 ,     0.05 , 'AGENT4'),
    (  5 , 300000.001 ,   500000 ,      0.3 ,     0.05 , 'AGENT4'),
    (  6 , 500000.001 , 10000000 ,     0.35 ,     0.05 , 'AGENT4'),
    (  7 ,          0 ,   10000 ,     0.15 ,     0.15 , 'AGENT1'),
    (  8 , 10000.001 , 15000 ,      0.2 ,     0.05 , 'AGENT1'),
    (  9 , 15000.001 , 20000 ,      0.3 ,     0.05 , 'AGENT1'),
    
    (  10 ,          0 ,   100000,     0.15 ,     0.15 , 'AGENT6'),
    (  11 , 100000.001 ,   200000 ,      0.2 ,     0.05 , 'AGENT6'),
    (  12 , 200000.001 ,   300000 ,     0.25 ,     0.05 , 'AGENT6'),
    (  13 , 300000.001 ,   500000 ,      0.3 ,     0.05 , 'AGENT6'),
    (  14 , 500000.001 , 10000000 ,     0.35 ,     0.05 , 'AGENT6')
    ;
    
    
    drop table if exists agent;
    create table agents ( Agent varchar(10) , APmonth int, FirstMonth int,  NGR  int, FIXEDCOM int, VARCOM2  decimal(10,2));
    truncate table agents;
    insert into agents values
    ( 'AGENT1' ,       3 ,          0 , 16802 ,        0 , 2520.327), 
    ( 'AGENT2' ,      17 ,          2 ,  7926 ,       60 , 1188.975), 
    ( 'AGENT3' ,       3 ,          0 ,  6541 ,        0 ,   981.15), 
    ( 'AGENT4' ,       4 ,          0 ,  5513 ,        0 ,   826.95), 
    ( 'AGENT5' ,       1 ,          0 ,     3 ,        0 ,      0.6), 
    ( 'AGENT6' ,       1 ,          0 , 750000,        0 ,     0.21);
    

    试试这个

    select u.sagent, u.ngr,
            u.amt1+u.amt2+u.amt3+u.amt4+u.amt5 as Commamt,
            u.com1+u.com2+u.com3+u.com4+u.com5 as Comm
    from 
    (
    SELECT T.SAGENT,T.NGR,t.comrate1,
                if(t.ngr <= t.maxtier1 , t.ngr ,t.maxtier1) amt1,
                ifnull(t.comrate1 * if(t.ngr <= t.maxtier1 , t.ngr ,t.maxtier1),0) com1,
                t.comrate2,
                if(t.ngr >= t.mintier2 and t.ngr <= t.maxtier2, t.ngr - t.maxtier1,if(t.ngr > t.mintier3,t.maxtier2 - t.maxtier1,0)) amt2,
                ifnull(t.comrate2 * if(t.ngr >= t.mintier2 and t.ngr <= t.maxtier2, t.ngr - t.maxtier1,if(t.ngr > t.mintier3,t.maxtier2 - t.maxtier1,0)),0) com2,
                t.comrate3,
               if(t.ngr >= t.mintier3 and t.ngr <= t.maxtier3, t.ngr - t.maxtier2,if(t.ngr > t.mintier4,t.maxtier3 - t.maxtier2,0)) amt3,
               ifnull(t.comrate3 * if(t.ngr >= t.mintier3 and t.ngr <= t.maxtier3, t.ngr - t.maxtier2,if(t.ngr > t.mintier4,t.maxtier3 - t.maxtier2,0)),0) com3,
                t.comrate4, 
                if(t.ngr >= t.mintier4 and t.ngr <= t.maxtier4, t.ngr - t.maxtier3,if(t.ngr > t.mintier5,t.maxtier4 - t.maxtier3,0)) amt4,
                ifnull(t.comrate4 * if(t.ngr >= t.mintier4 and t.ngr <= t.maxtier4, t.ngr - t.maxtier3,if(t.ngr > t.mintier5,t.maxtier4 - t.maxtier3,0)),0) com4,
                t.comrate5,
                if(t.ngr >= t.mintier5 and t.ngr <= t.maxtier5, t.ngr - t.maxtier4,if(t.ngr > 9999999999,t.maxtier5 - t.maxtier4,0)) amt5,
                ifnull(t.comrate5 * if(t.ngr >= t.mintier5 and t.ngr <= t.maxtier5, t.ngr - t.maxtier4,if(t.ngr > 9999999999,t.maxtier5 - t.maxtier4,0)),0) com5
    FROM
    (
    select s.agent SAGENT,a.*,
             max(case when s.rn = 1 then s.tier_min end) mintier1,
             max(case when s.rn = 1 then s.tier_max end) maxtier1,
             max(case when s.rn = 1 then s.com_rate end) comrate1,
             max(case when s.rn = 2 then s.tier_min end) mintier2,
             max(case when s.rn = 2 then s.tier_max end) maxtier2,
             max(case when s.rn = 2 then s.com_rate end) comrate2,
             max(case when s.rn = 3 then s.tier_min end) mintier3,
             max(case when s.rn = 3 then s.tier_max end) maxtier3,
             max(case when s.rn = 3 then s.com_rate end) comrate3,
             max(case when s.rn = 4 then s.tier_min end) mintier4,
             max(case when s.rn = 4 then s.tier_max end) maxtier4,
             max(case when s.rn = 4 then s.com_rate end) comrate4,
             max(case when s.rn = 5 then s.tier_min end) mintier5,
             max(case when s.rn = 5 then s.tier_max end) maxtier5,
             max(case when s.rn = 5 then s.com_rate end) comrate5
    from 
    (
    select t.*,
            if (t.agent <> @p ,@rn:=1,@rn:=@rn+1) rn,
            @p:=t.agent 
    from (select @rn:=0,@p:='') rn,t 
    order by t.agent, t.id
    ) s 
    join agents a on a.agent = s.agent
    group by s.agent
    ) T
    ) u
    

    请注意,我假设最大速率数为 5 子查询 s 为每个代理 (1-5) 的每个费率分配一个行号,然后将其连接并旋转以创建每个代理 1 行。然后子查询 t 计算出落入每个佣金范围的金额和委员会。外部查询然后简单地将落入每个波段的金额作为交叉检查和最终佣金金额相加。

    【讨论】:

    • 您好,感谢您的解决方案。我正在尝试在没有严格的最大阈值的情况下实现这一目标。
    • 如果您没有最大限制,那么您将不得不使用动态 sql - 但真的会超过 10 个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多