I am trying to derive sales commissions based on a sharing schedule. I have 2 problems:
- How to write a query without using the built-in subquery
- How to withdraw the seller and their remaining commission if they did not divide it by 100%.
This is what I have so far:
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
Expected results:
SalesPerson Commission Amy 250.00 Lisa 350.00 Wendy 240.00 Mike 660.00
source share