Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a winform with a access database. I need to retrieve the top 10 repeated numbers in a column. The code below give me the first 10 numbers in the column.




C#
private void btn_Top_10_Click(object sender, EventArgs e)
        {
            string queryString = "SELECT ListSheetID, Part FROM Post";
            queryString = "select top 10 ListSheetID,Part from Post";
            loadDataGrid(queryString);
        }


What I have tried:

The example above and a lot on looking and reading on the net.
Posted
Updated 20-Dec-17 10:37am

1 solution

Quick and dirty method;

SQL
SELECT TOP 10 * FROM
    (SELECT ListSheetId, Part, COUNT(*) as RCount FROM Post GROUP BY ListSheetId, Part) AS MyTable
ORDER BY RCount DESC


Assume the following data
ListSheetId, Part
12, Wheel
10, Axle
1, Seat
12, Wheel
10, Axle
1, Seat
1, Seat
1, Seat
10, Axle
1, Seat
1, Seat
12, Wheel
12, Wheel
12, Wheel
1, Seat
1, Seat
1, Seat
1, Seat
1, Seat

How this works.
The Inner Query retrieves a list of ListSheetId, Part & a count of the number of Sheets & Parts
Result Set:
ListSheetId, Part, Count
12, Wheel, 5
10, Axle, 3
1, Seat, 11
The outer query retrieves the top 10 results after ordering the inner query by the Count column. If the above query was changed to SELECT TOP 2 then the result set would be;
ListSheetId, Part, Count
1, Seat, 11
12, Wheel, 5

Additional information;
GROUP BY creates distinct lists, assume the following data;
ListSheetId, Part, Count
12, Wheel, 5
10, Axle, 3
1, Seat, 11
12, Axle, 8
10, Seat, 4

If the above query was changed to SELECT TOP 4 then the result set would be;
ListSheetId, Part, Count
1, Seat, 11
12, Axle, 8
12, Wheel, 5
10, Seat, 4

This is because GROUP BY identifies the distinct values across all columns in the group by clause - NOTE: Count is not included as it is an aggregate method and is not included in the Group By clause.
Therefore 1 & Seat, 12 & Axle, 12 & Wheel & 10 & Seat are the first 4 distinct values after applying the Sort method

If this is not what you need please improve your question by adding example data & result sets


Kind Regards
 
Share this answer
 
v2
Comments
Member 12349103 20-Dec-17 17:15pm    
That's what I need to accomplish bur I cant get that code to work at all.
an0ther1 20-Dec-17 18:34pm    
So what happens?
If you execute the query in Access, does it work as expected?
If not then you will need to work out why it doesn't give you what you want.
If it does then you need to debug your WinForm application.
Refer updated solution for additional information
Member 12349103 20-Dec-17 18:41pm    
This is for my button click? Every word has an error, apparently I dont know where to put this.
an0ther1 20-Dec-17 19:34pm    
Step 1: Create a new query in Access & confirm it returns the results you expect.
Step 2: Replace you button click event as follows;
string queryString = "My Tested Query HERE";
LoadDataGrid(queryString);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900