Top N Clients Accounting for Half of the Total Sales in a Certain Year

l  Problem

The table below is the historic sales contracts of an enterprise:

The table below is the Client table:

Among clients ranked by sales value in a certain year, the top N clients accounting for half of the total sales value are key accounts. Please list the key accounts of this enterprise in the year of 1998.


l  Tip

General steps: Firstly, group the Contract table by Client, compute the total sales value achieved by each client and arrange the results in descending order, then work out the half value of the total sales value. Finally, scan this table,aggregating sales value cumulatively till reaching half of the total sales value. Then, these clients whose sales account for half of the total sales values are the key accounts.

1.  In order to facilitate the client name retrieval, replace the Client field in the Contract table with the corresponding Client records.

2.  Select out the Contract records in the 1998

3.  Group the screened table by Client. Then, the sales contracts of each client will be grouped together and a new table sequence will be generated. Next sum up the amount of each group.

4.  Sort this table sequence by sales amount in descending order

5.  Compute the total sales amount of all clients, that is, half of the total annual sales value.

6.  Traverse every record top-down with pselect function, and sum up the amounts cumulatively till the value reaches half of the total sum. Once traverse stops, you will get the number of key accounts- N.

7.  Retrieve the names of top N clients, who are the key accounts of the year of 1998.


l  Code 






Contract table



Client table


>A2.keys(ID), A1.switch(Client,A2)

Update the client field of contract table to client record


Screen out the sales record in 1998


Group by Client in the table, and sum up the amount of each group



Sort by amount in descending order



Half sales volume of this year



Temporary variable of aggregate amount



Find the half position of the aggregate amount



Retrieve the names and they are the names of big client of this year


l  Result