riffrack
asked on
Getting the TOP X records per productgroup
Hi experts
We have a product-group table that lists per productgroup the amount of products to be shown. E.g.
ProductGroup NumberOfProducts
Soap 10
Shampoo 20
Toothpaste 15
So now we would like to show the top x products per productgroup ordered by price.
We have tried using SELECT TOP (x) or ROW_NUMBER, but have not yet found a working solution for this.
We have a product-group table that lists per productgroup the amount of products to be shown. E.g.
ProductGroup NumberOfProducts
Soap 10
Shampoo 20
Toothpaste 15
So now we would like to show the top x products per productgroup ordered by price.
We have tried using SELECT TOP (x) or ROW_NUMBER, but have not yet found a working solution for this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Absolutely perfect, this solution works for my tables containg more products than are to be shown.
I created a test table variable to work with (you can replace it with your table):
Open in new window
This will output as your example.Then you may "partition" your query by "ProductGroup" and order the rows by Price:
Open in new window
This will output:Open in new window
Regards.