Joe Celko s SQL for Smarties - Advanced SQL Programming P57. In the SQL database community, Joe Celko is a well-known columnist and purveyor of valuable insights. In Joe Celko's SQL for Smarties: Advanced SQL Programming, he picks up where basic SQL training and experience leaves many database professionals and offers tips, techniques, and explanations that help readers extend their capabilities to top-tier SQL programming. Although Celko denies that the book is about database theory, he nevertheless alludes to theory often to buttress his practical points. This title is not for novices, as the author points out. Instead, its intended. | 532 CHAPTER 23 STATISTICS IN SQL CREATE TABLE Sales salesman CHAR 10 client_name CHAR 10 sales_amount DECIMAL 9 2 NOT NULL PRIMARY KEY salesman client_name The problem is to show each salesman his client the amount of that sale what percentage of his total sales volume that one sale represents and the cumulative percentage of his total sales we have reached at that point. We will sort the clients from the largest amount to the smallest. This problem is based on a salesman s report originally written for a small commercial printing company. The idea was to show the salesmen where their business was coming from and to persuade them to give up their smaller accounts defined as the lower 20 to new salesmen. The report lets the salesman run his finger down the page and see which customers represented the top 80 of his income. We can use derived tables to build layers of aggregation in the same query. SELECT 100 AS percent_of_total SUM 100 AS cum_percent FROM Sales AS S0 INNER JOIN Sales AS S1 ON INNER JOIN SELECT SUM FROM Sales AS S2 GROUP BY AS ST salesman salesman_total ON GROUP BY However ifyour SQL allows subqueries in the SELECT clause but not in the FROM clause you can fake it with this query Cumulative Statistics 533 SELECT SELECT SUM FROM Sales AS S1 WHERE AS percentage_of_total SELECT SUM FROM Sales AS S3 WHERE AND OR AND SELECT SUM FROM Sales AS S2 WHERE AS cum_percent FROM Sales AS S0 This query will probably run like glue. .