|
SQL Puzzlers, by Joe Celko
Joe Celko is an independent consultant in Austin, Texas, and the author of SQL Puzzles and Answers (2006), Joe Celko's SQL for Smarties: Advanced SQL Programming (2005), and Joe Celko's Trees and Hierarchies in SQL for Smarties (2004). See More by Joe Celko Celko SQL Puzzle: Calculating Commissions
I discussed a number of ways to split commissions among multiple salespeople in this article, but can you come up with other ways to keep track of the commission amounts? I'll give you a hint; ask yourself, "what is the simplest fact in this problem?" One way of spotting a design problem is when you have to use a VIEW or CTE over and over to answer basic questions. This implies that the schema has spread information over many tables that should be in one table or that it has aggregated data in one row that should be split out into multiple rows. The schema is a mess. Attributes of a sales commission are split over multiple tables, so we ought to consolidate things in one place. The design below copies paper input forms that showed the total but did not breakdown the associated commissions. CREATE TABLE SalesCommissions The total of each sale has to be derived in a VIEW. The VIEW will now balance to the commission amounts. CREATE VIEW Sales (sale_nbr, cust_id, sales_amt) One trick is to put the division of the spoils in a stored procedure that inserts an equal division into the SalesCommissions instead of trying to get it into a VIEW. CREATE PROCEDURE PostCommissions INSERT INTO SalesCommissions It would be nice to make X into a CTE, but I do not know of any SQL product that can do that yet. This procedure is good for up to a five-away split. The pattern for adding more and more salespeople is obvious. The use of SELECT DISTINCT will also allow a salesperson's id to be input twice without causing an error. In the real world, there are probably more business rules for dividing the Spoils based on seniority, the items purchased and so forth. E-MAIL | SLASHDOT | DIGG This is a public forum. CMP Technology and its affiliates are not responsible for and do not control what is posted herein. CMP Technology makes no warranties or guarantees concerning any advice dispensed by its staff members or readers. Community standards in this comment area do not permit hate language, excessive profanity, or other patently offensive language. Please be aware that all information posted to this comment area becomes the property of CMP Media LLC and may be edited and republished in print or electronic format as outlined in CMP Technology's Terms of Service. Important Note: This comment area is NOT intended for commercial messages or solicitations of business.
|
Blog Channels
The Brain Food Blogger SQL Puzzlers by Joe Celkoon Enterprise App Development on Changing the Enterprise by Shawn Shell by Kas Thomas Product Maven Subscribe to RSS feed of all blogs Archives
|
| |||||||||||||||||||||||||||||||





















