Finding the Top n Within a Group

Question:
[Joe Celko’s Best Salesmen by Territory Puzzle]

Martin Tillinger posted this problem on the MSAccess forum of CompuServe. How do you get the top three salesmen in each territory, given a table of SalesReport that looks like this?

    CREATE TABLE SalesReport        (name CHAR(20) NOT NULL REFERENCES Salesmen(name),        territory INTEGER NOT NULL,        amount DECIMAL (8,2) NOT NULL);

Answer:
The following query gives the highest SalesReport the rank of 1, the next highest the rank of 2 and so forth. The query can handle ties within a rank. If the territory has less than (n) members, then it will return all the members.

    SELECT S0.territory, S0.name, S0.amount,            (SELECT COUNT(DISTINCT amount)                FROM SalesReport AS S1                WHERE (S1.amount >= S0.amount)                    AND (S1.territory = S0.territory)) AS rank        FROM SalesReport AS S0        WHERE rank <= 3; -- adjustable parameter
The problem was that Mr. Tillinger was working with Microsoft Access, which does not have a COUNT (DISTINCT x) function, so he tried to do the query without it:
    SELECT S0.territory, S0.name, S0.amount,            (SELECT COUNT(amount)                FROM SalesReport AS S1                WHERE (S1.amount >= S0.amount)                    AND (S1.territory = S0.territory)) AS rank        FROM SalesReport AS S0        WHERE rank <= 3; -- adjustable parameter
The problem is that ties get decided wrong. For example given:
    SalesReport    name  territory  amount     =======================    Wilson      1   $990.00    Smith       1   $950.00    Richards    1   $800.00    Quinn       1   $700.00    Parker      1   $345.00    Jones       1   $345.00    Hubbard     1   $345.00    Date        1   $200.00    Codd        1   $200.00    Blake       1   $100.00
The correct answer is:
    DISTINCT Result    name  territory  amount rank    ============================    Wilson      1   $990.00  1    Smith       1   $950.00  2    Richards    1   $800.00  3    Quinn       1   $700.00  4    Parker      1   $345.00  5    Jones       1   $345.00  5    Hubbard     1   $345.00  5    Date        1   $200.00  6    Codd        1   $200.00  6    Blake       1   $100.00  7
The un-DISTINCT answer is
    Un-DISTINCT Result    name  territory  amount rank    ============================    Wilson      1   $990.00  1    Smith       1   $950.00  2    Richards    1   $800.00  3    Quinn       1   $700.00  4    Parker      1   $345.00  6    Jones       1   $345.00  6    Hubbard     1   $345.00  6    Date        1   $200.00  8    Codd        1   $200.00  8    Blake       1   $100.00  9
The ties cause gaps and mess up the ability you had to use the rank column to select (n) items. There are also other problems in certain statistics due to the gaps.

Puzzle provided courtesy of:
Joe Celko
[email protected]

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS