Celko on SQL: Dividing the SpoilsIn this database programming challenge, SQL guru Joe Celko explores various ways to split commissions among multiple sales people. By Joe Celko September 2, 2008
You are given two tables that both deal with sales transactions. One table shows which sales people were involved with each customer and sale. The second table shows the total amount of each sale and the commissions that have to be split among the sales people involved in each transaction. Just how do you calculate the commissions? This problem, which was originally posted on a newsgroup site, presents an interesting challenge.
What we want to do is join the two tables on sale_nbr and cust_id so we can see how much each salesperson gets for each sale. We are assuming that if (n) salespeople are assigned to a sale, we split the transaction amount among them. Here is a guess at the DDL as it was described in the post. The PRIMARY KEYs had to be determined by the data shown.
CREATE TABLE SalesTickets I have no idea why there is a cust_id column in both tables, so I assumed that it is part of the primary key, although it is a bit strange that one sale ticket involves multiple customers. Perhaps each customer's sales are numbered within his account instead? In any case, you can use a query like this:
SELECT T.sale_nbr, T.cust_id, T.salesperson_id, It's a little more difficult if the transaction amount is not divisible by the number of sales persons, so the commissions will not be exactly the same. For example, if you have a $1000.00 sale to divide among 3 salespersons, each will get $333.3333, with a total of $999.9999, not $1000.00 unless you round it up. Just let the boss worry about the pennies for now. Did you notice that we created a Table X in the last solution? There is a pretty good chance that the derived table will be materialized by the optimizer. It would sure be nice to avoid creating tables on the fly. Here is another approach, using a scalar subquery to get the divisor.
SELECT T.sale_nbr, T.cust_id,
|
New on the BLOG
Is Oracle An Apps Company that Sells Hardware, or a Hardware Company that Dabbles in Apps?
09. 8.2010
Read more from Josh Greenbaum >>
Facebook, LinkedIn, Trip Advisor, and Twitter -- social media -- are almost incidental, replaceable tomorrow if another platform proves more attractive, powerful, and agile. It's content that is king, especially "smart content" that allows producers and consumers alike to find the greatest value in online and enterprise content. 09. 8.2010 Read more from Seth Grimes >> Using Virtualization for "Smart Multi-Tenancy" 09. 8.2010
Read more from Rajan Chandras >> Most Popular This Week
Intelligent Enterprise Newsletters
Subscribe Here:
| |||||||||||||||||
|
|



