Chris Posted December 15, 2009 Report Share Posted December 15, 2009 The table name is: wasteexportThere are 5 fields: Site, Pid, Code, Des, QtySite is a number (can be text)Pid is textCode is a numberDes is textQty is a numberIn 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.001001001002002003etcI've tried this in SQL:SELECT TOP 20 PERCENT wasteexport.site, wasteexport.pid, wasteexport.code, wasteexport.des, wasteexport.qtyFROM wasteexportORDER 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? Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 16, 2009 Report Share Posted December 16, 2009 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... Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted December 16, 2009 Report Share Posted December 16, 2009 I just had a closer look at thishttp://technet.microsoft.com/en-us/library/ms189463.aspxIt appears as if the TOP clause is related to the ORDER BY clause. Can you try it with only one column name (Code) in the ORDER BY ? Quote Link to comment Share on other sites More sharing options...
Chris Posted December 16, 2009 Author Report Share Posted December 16, 2009 Yes there are duplicate Codes, but the other information in the record makes the record unique.I'll try the order thing....and get back to you. Quote Link to comment Share on other sites More sharing options...
Chris Posted December 17, 2009 Author Report Share Posted December 17, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.