Jump to content

MS Access 2003 - Top 20 Percent


Recommended Posts

The table name is: wasteexport

There are 5 fields: Site, Pid, Code, Des, Qty

Site is a number (can be text)

Pid is text

Code is a number

Des is text

Qty is a number

In the table the Code field is Ascending and the Qty field is Descending.

There could be several hundred of the same Code field i.e. Try to image there being 100x 001, 500x 002 etc.

001

001

001

002

002

003

etc

I've tried this in SQL:

SELECT TOP 20 PERCENT wasteexport.site, wasteexport.pid, wasteexport.code, wasteexport.des, wasteexport.qty
FROM wasteexport
ORDER BY wasteexport.code, wasteexport.qty DESC;

However this only returns the top 20 percent of data in the table, i.e 8036 records = returns 20% of that which is the first 1608 records.

I want to be able to select the top 20 percent of each Code data that it finds. As the Qty field is already descending it does not matter about this or does it - it needs to be descending.

For example if there are 500x 001 it should return the first 100. If there are 5x 001 it would only return the first one.

How do I specify the top 20 percent on the Code field?

Link to comment
Share on other sites

I don't know that "TOP nn PERCENT" clause of SQL, but I can understand its meaning.

What I would try is a subquery on the Code column, then use this result to join the rest of the data.

I currently don't have MS Access available (that computer stands disconnected on the floor). But let me know if you need help coding the subquery.

P.S. is the Code column UNIQUE (i.e. no duplicate values)? If not, that may become difficult...

Link to comment
Share on other sites

I didn't get the chance to do this in Access today but I have a feeling it wouldn't work as you are only ascending or descending the field by using ORDER BY.

The good news is I did it in Excel, but it involved a few formulas to get it done. However this is long winded.

I'll keep trying.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy