使用共享计划计算销售佣金

时间:2017-08-23 18:05:13

标签: sql-server tsql

我试图根据共享时间表输出销售佣金。我有两个问题:

  1. 如何在不使用内联子查询的情况下编写查询
  2. 如果没有分享100%的销售人员及其剩余佣金,如何输出。
  3. 这是我到目前为止所做的:

    DECLARE @SALES TABLE
                   (
                       Id INT,
                       SalesPerson VARCHAR(10),
                       Commission MONEY
                   )
    
    INSERT INTO @SALES VALUES(1,'Amy',    100.00)
    INSERT INTO @SALES VALUES(2,'Lisa',   200.00)
    INSERT INTO @SALES VALUES(3,'Joe',    300.00)
    INSERT INTO @SALES VALUES(4,'Wendy',  400.00)
    INSERT INTO @SALES VALUES(5,'Mike',   500.00)
    
    
    DECLARE @SHARE_SCHEDULE TABLE
                            (
                                GIVER INT,
                                TAKER INT,
                                PERCENTAGE DECIMAL(9,2)
                            )
    
    INSERT INTO @SHARE_SCHEDULE VALUES(3, 1, .5)
    INSERT INTO @SHARE_SCHEDULE VALUES(3, 2, .5)
    INSERT INTO @SHARE_SCHEDULE VALUES(4, 5, .4)
    
    SELECT 
        S.SalesPerson,
        S.Commission + (SELECT SUM(Commission) 
                        FROM @SALES 
                        WHERE ID = SS.GIVER) * SS.PERCENTAGE Commission
     FROM @SALES S
     JOIN @SHARE_SCHEDULE SS ON SS.TAKER = S.ID
    

    预期结果:

    SalesPerson Commission
    Amy         250.00
    Lisa        350.00
    Wendy       240.00
    Mike        660.00
    

5 个答案:

答案 0 :(得分:1)

SELECT 
    S.Id,
    S.SalesPerson, 
    S.Commission + ISNULL(G.GIVEN,0) +ISNULL(T.TAKEN,0) Commission 
FROM @SALES S
    LEFT JOIN
    (
        SELECT 
            GIVER, 
            -SUM(PERCENTAGE*S.Commission)  GIVEN
        FROM @SHARE_SCHEDULE SS 
            JOIN @SALES S
                ON SS.GIVER=S.Id
        GROUP BY GIVER
    ) G ON G.GIVER=S.Id
    LEFT JOIN
    ( 
         SELECT 
            TAKER,
            SUM(PERCENTAGE*S.Commission)  TAKEN
         FROM @SHARE_SCHEDULE SS 
            JOIN @SALES S
                ON SS.GIVER=S.Id
         GROUP BY TAKER
    )T ON T.TAKER=S.Id
WHERE S.Commission + ISNULL(G.GIVEN,0) >0

See working demo

答案 1 :(得分:1)

需要将其分为三个不同的部分:

  1. 原始表销售
  2. 给予值的送礼者
  3. 收件人的收件人
  4. 然后组合这些以获得所需的输出。

    select 
    S.SalesPerson,
           (S.Commission  --original commision
               +       
             (ISNULL(tempTaker.Commission, 0)) -- plus recevied as TAKER
             -
             (ISNULL(tempGiver.Commission, 0))  -- minus paid as GIVER
    
            )
          Commission
    
    from @SALES s
    
    --tempTaker 
    left outer join 
    (
        select ss1.TAKER, SUM(ss1.PERCENTAGE * s1.Commission) Commission 
        FROM @SALES S1 
        inner join @SHARE_SCHEDULE ss1 on s1.Id = ss1.GIVER
        group by ss1.TAKER
    ) tempTaker on tempTaker.TAKER = s.id
    
    --tempGiver
    left outer join 
    (
        select ss1.GIVER, SUM(ss1.PERCENTAGE * s1.Commission) Commission 
        FROM @SALES S1 
        inner join @SHARE_SCHEDULE ss1 on s1.Id = ss1.GIVER
        group by ss1.GIVER
    
    ) tempGiver on tempGiver.GIVER = s.id
    
    --filter, the final value should be greater than ZERO.
    where (s.Commission 
            +        
             ISNULL(tempTaker.Commission, 0) 
            -
             ISNULL(tempGiver.Commission, 0)
             )
              > 0
    

答案 2 :(得分:1)

我已经完成了一些CTE,一个用于名称,他们给出的金额,以及他们从其他人那里收到的金额。我还将佣金金额格式化为两位小数。

;WITH base AS (
 SELECT 
    SalesPerson
    ,Commission
    ,id
 FROM @SALES
 ),
 commission_given AS (
    SELECT
        S.GIVER
        ,SUM(base.Commission * s.PERCENTAGE) * -1 commission_given
    FROM @SHARE_SCHEDULE s
    INNER JOIN base ON s.GIVER = base.Id
    GROUP BY S.GIVER, S.PERCENTAGE
 ),
  commission_received AS (
    SELECT
        S.taker
        ,SUM(base.Commission * s.PERCENTAGE)as commission_received
    FROM @SHARE_SCHEDULE s
    INNER JOIN base ON s.giver = base.Id
    GROUP BY S.taker, S.PERCENTAGE
 ),
 TOTAL_COMMISSION AS (
    SELECT
        BASE.SalesPerson
        ,BASE.Commission + ISNULL(cs.commission_given,0) + ISNULL(cr.commission_received,0) AS TOTAL_COMMISSION
    FROM BASE
    LEFT OUTER JOIN commission_given AS cs ON BASE.Id = cs.GIVER
    LEFT OUTER JOIN commission_received cr on base.id = cr.taker
 )
 SELECT 
    SalesPerson
    ,CAST(TOTAL_COMMISSION AS NUMERIC(10,2)) AS TOTAL_COMMISSION 
 FROM TOTAL_COMMISSION
 WHERE TOTAL_COMMISSION > 0

工作演示: http://rextester.com/live/NWQIR26309

答案 3 :(得分:1)

with Q as
(
    select S.*, coalesce(J.share, 0) received, coalesce(K.share, 0) given from SALES S

    outer apply
    (
        select sum(H.PERCENTAGE * L.Commission) as share from SHARE_SCHEDULE H 
            inner join SALES L on L.Id = H.GIVER
            where H.TAKER = S.Id

    ) J

    outer apply
    (
        select sum(H.PERCENTAGE * L.Commission) as share from SHARE_SCHEDULE H 
            inner join SALES L on L.Id = H.GIVER
            where H.GIVER = S.Id
    ) K
)

select SalesPerson, (Commission + received - given) TotalCommission from Q 
    where (Commission + received - given) > 0

J 是销售人员收到的所有共享佣金

的总和

K 是销售人员提供的所有共享佣金的总和

中间结果(select * from Q)如下所示:

Id  SalesPerson Commission received     given
--- ----------- ---------- ------------ -----------
1   Amy         100.00     150.000000   0.000000
2   Lisa        200.00     150.000000   0.000000
3   Joe         300.00     0.000000     300.000000
4   Wendy       400.00     0.000000     160.000000
5   Mike        500.00     160.000000   0.000000

最终结果如下:

SalesPerson     TotalCommission (= commission + received - given)
-----------     ---------------
Amy             250.000000
Lisa            350.000000
Wendy           240.000000
Mike            660.000000

Rextester Demo

答案 4 :(得分:1)

我相信这可以解决您的问题:

SELECT 
    S.SalesPerson,
    MAX(S.Commission) 
        + Sum(IsNull(ResPlus.Value,0)) 
        - Sum(IsNull(ResSubt.Value,0)) Commission
FROM @SALES S
LEFT JOIN @SHARE_SCHEDULE SS ON SS.TAKER = S.ID
LEFT JOIN @SHARE_SCHEDULE SS2 ON SS2.GIVER = S.ID 
CROSS APPLY (SELECT SS.PERCENTAGE * SUM(Commission) FROM @SALES WHERE ID = SS.GIVER) ResPlus(Value)
CROSS APPLY (SELECT SS2.PERCENTAGE * SUM(Commission) FROM @SALES WHERE ID = SS2.GIVER) ResSubt(Value)
group by S.SalesPerson, S.ID
having MAX(S.Commission) + Sum(IsNull(ResPlus.Value,0)) - Sum(IsNull(ResSubt.Value,0)) > 0
order by S.ID

Live demo