mardi 5 mai 2015

Complex Sql Query for Sales Commison Calculation Multi Level

I'm trying to get employees report which shows their commission.

CREATE TABLE [dbo].[Employee] (
    [Id]               INT           NOT NULL,
    [firstname]        VARCHAR (100) NULL,
    [lastname]         VARCHAR (100) NULL,
    [promoterID]         INT           NULL,
    [titleID]         INT           NULL,   
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

insert into employee(id,firstname,lastname,promoterid) values(1,'King1','Knight1',null,1)
insert into employee(id,firstname,lastname,promoterid) values(2,'King2','Knight2',1,2)
insert into employee(id,firstname,lastname,promoterid) values(3,'King3','Knight3',1,1)
insert into employee(id,firstname,lastname,promoterid) values(4,'King4','Knight4',2,3)
insert into employee(id,firstname,lastname,promoterid) values(5,'King5','Knight5',4,2)



CREATE TABLE [dbo].[sales_product] (
    [Id]               INT           IDENTITY (1, 1) NOT NULL,
    [customerid]         INT           NULL,
    [employeeid]          INT           NULL,
    [commision]        FLOAT (53)    NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);


insert into sales_product(id,customerid,employeeid,commision) values(1,2,5,100)
insert into sales_product(id,customerid,employeeid,commision) values(2,3,3,200)
insert into sales_product(id,customerid,employeeid,commision) values(3,5,2,100)
insert into sales_product(id,customerid,employeeid,commision) values(4,6,5,400)
insert into sales_product(id,customerid,employeeid,commision) values(5,7,1,500)

CREATE TABLE [dbo].[title] (
    [Id]          INT           NOT NULL,
    [title_code]  VARCHAR (200) NULL,
    [onA]         INT           NULL,
    [onEC]        INT           NULL,
    [onJSC]       INT           NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

insert into title(id,title_code,<Dynamic Columns that gets created on adding new title whose name is "on" + title_code>) values(1,'A',20,30,10)
insert into title(id,title_code,<Dynamic Columns that gets created on adding new title whose name is "on" + title_code>) values(2,'EC',20,30,10)
insert into title(id,title_code,<Dynamic Columns that gets created on adding new title whose name is "on" + title_code>) values(3,'JSC',20,30,10)
insert into title(id,title_code,<Dynamic Columns that gets created on adding new title whose name is "on" + title_code>) values(4,'SSC',20,30,10,0)

Required Output:

firstname   lastname    totalcommison(employee commision + sum of (junior commsions*commision % based on title of, i.e if employee title_code is A than it will look for % on juniors title code ie onEC or JSC... which we get by getting juniors title code))

So far i'm able to get all the boses of employee in heirarchy

WITH EmployeeAncestor(id, firstname, promoterid) 
AS 
( 
  select id, firstname, promoter from employee WHERE id = 4

  union all 

  select m.id, m.firstname, m.promoterid 
  from EmployeeAncestor CTE,employeem 
  WHERE CTE.promoter = m.id 
 ) 
SELECT id, firstname, promoterid FROM EmployeeAncestor ORDER BY id ASC

I have following tables:

  1. sales_product - where i store employee sales and commission he gets

Column Names : salesid, employeeid, productid, commision

  1. Employee - this table stores employee detail, title id and promoter Id i.e, boss of the employee.

Column Names : employeeid, promoterid(employee id of manager), firstname, lastname, titleid

  1. title - where i store titles of the employee for example general manger, junior sales consultant etc and commission % on the different titles

For example

titleId     Title              CommisiononJuniorSalesCons     CommisiononSeniorSalesCons

 1.  General Manager                   10                          50  
 2. Junior Sales Cons                   0                           5

Now I want to create a SQL query and display it in a table where i get employee name and total commission he gets on his juniors. The commission will be calculated by multiplying the commission on product from sales_product table and % commission on employee sale from title and then we sum that commission and we get total commission earned by the employee.

Aucun commentaire:

Enregistrer un commentaire