How would I show, for example, the 10 most populous products in a table?
I have a sales table for example, that contains sales for 20 beers, 10 nachos, 5 peanuts, 2 hotdogs and 4 breakfasts.
Is there a way to automatically calculate and see which is the most popular, and show them in descending order?
edit:
My table is as follows
Tablename: sales
Fields:
- product (varchar)
- quantity (int)
- cost (decimal)
- saledate (date)
One sale record could count as more than one sale of a product, because of the quantity field.
I also have a guests table, which only has the fields name and country, and was wanting to show say, the top 5 most populous countries in the table.
Does this work for you?
This counts the products in the inner query (it wasn’t clear if your table already stores counts, but it’s not common so I assumed you didn’t) and then orders them in the outer query.This also assumes your products have a productID field.
UPDATE
If your table actually tracks sales counts you could do something like this:
Very similar to the previous one but you are taking the sum of products sold from multiple sales items, rather than the count of products.