I am using Couchbase and N1QL.
Let's say I have a table of products. Products have a categoryId
and a price
among other attributes:
id | name | categoryId | price |
---|---|---|---|
1 | product 1 | cat1 | 5 |
2 | product 2 | cat2 | 100 |
3 | product 3 | cat1 | 25 |
I know how to get the 5 most expensive products for a single category. I would do something like this:
SELECT * FROM products
WHERE categoryId = 'cat1'
ORDER BY price DESC
LIMIT 5
But how would I query for the top 5 most expensive products per category? I know I can do a WHERE categoryId IN ['cat1', 'cat2']
but this would result in the top5 products among those categories, and not per category.
Any couchbase or N1QL experts that can help me out?
Thanks in advance!