多级营销在一个查询MS SQL中选择并插入Commision

时间:2018-01-25 06:14:56

标签: sql sql-server

/home/ahmadi/Qt5.9.3/5.9.3/gcc_64/bin/qmake
树1是我的多层次营销树

enter image description here 示例:如果BBA获得销售佣金100美元,BBA获得60%60美元,BA获得35%35美元,B获得5美元,公司获得销售佣金10美元。

等级5最低等级

当销售成功时,将插入到tbl_trans,佣金将在月末计算,读取tbl_trans并计算销售佣金除以所有推荐并存储在tbl_comm中。

tbl_trans的一个交易将根据tbl_user创建4个佣金并存储在tbl_comm中。

这是示例数据和表格,请帮助我http://sqlfiddle.com/#!18/8585c

这是我试过的,但是没有任何想法继续

--'------------------------------------------
--' TREE 1
--'------------------------------------------
--'                 Company
--'             --------|-------
--'             |              |
--'            A               B
--'       -----|-----      -----|--------
--'       |          |     |            |
--'      AA         AB     BA          BB
--'                      ---|---     ---|---
--'                     |       |    |      |
--'                    BBA    BBB   BBC    BBD
--'------------------------------------------

2 个答案:

答案 0 :(得分:0)

请找到以下解决方案。

term aggregation documentation

    CREATE TABLE tbl_user(
    [User_ID] INTEGER NOT NULL,
    [User_Username] [nvarchar](30) NULL,
    [User_UplineID] INTEGER NULL,
    [User_GivenComm1] INTEGER NULL,
    PRIMARY KEY (User_ID)
);

INSERT INTO tbl_user
    ([User_ID], [User_Username],[User_UplineID],[User_GivenComm1])
VALUES
    (1, 'Company',0,10),
    (2, 'B',1,5),
    (3, 'BA',2,25),
    (4, 'BBA',3,60);

CREATE TABLE tbl_trans(
    [trans_ID] INTEGER NOT NULL,
    [trans_UserID] INTEGER NULL,
    [trans_SalesComm] [INTEGER] NULL,
    PRIMARY KEY (trans_ID)
);

INSERT INTO tbl_trans
    ([trans_ID], [trans_UserID],[trans_SalesComm])
VALUES
    (1, 4,125);

CREATE TABLE tbl_comm(
    [comm_ID] INTEGER identity(1,1) NOT NULL,
    [trans_UserID] INTEGER NULL,
    [trans_ID] INTEGER NULL,
    [trans_CommAmt] [INTEGER] NULL,
    PRIMARY KEY (comm_ID)
);

获取父子和计算份额的CTE

;with myTree as (
   select User_ID,user_username,user_uplineID,trans_SalesComm, User_ID as sourceID, trans_id,User_GivenComm1 from tbl_user y
         inner join tbl_trans x on x.trans_userID = y.user_ID

    union all
    select u.User_ID,u.user_username,u.user_uplineID,t.trans_SalesComm, t.sourceID as sourceID, t.trans_Id,u.User_GivenComm1
          from myTree t
          inner join tbl_user u on
                t.user_uplineID = u.User_ID
 )
  insert into tbl_comm (trans_userID,trans_ID,trans_CommAmt)
  select 
    l.user_ID,
    l.trans_id,
    (l.User_GivenComm1 * 0.01) *  trans_SalesComm
    from myTree l;

  select * from tbl_comm;

答案 1 :(得分:0)

你可以试试这个。

;WITH CTE AS(
    SELECT *, [User_ID] RelatedUserID   FROM tbl_user 
    UNION ALL
    SELECT B.*, CTE.RelatedUserID FROM tbl_user B 
        INNER JOIN CTE ON B.[User_ID]= CTE.User_UplineID
)
INSERT INTO tbl_comm ([trans_UserID], [trans_ID], [trans_CommAmt])
SELECT CTE.[User_ID], trans_ID,
    CAST( User_GivenComm1 AS decimal(18,2) ) 
        * ( trans_SalesComm / 
            ( SUM( CAST( User_GivenComm1 AS decimal(18,2) ) ) OVER ( PARTITION BY trans_ID,RelatedUserID) ) )
as commission
  FROM CTE
    inner join tbl_trans T ON T.trans_UserID = CTE.RelatedUserID

结果:

comm_ID     trans_UserID trans_ID    trans_CommAmt
----------- ------------ ----------- -------------
1           4            1           60
2           3            1           25
3           2            1           5
4           1            1           10