sql - What are the products of each categories fewest requested by customers? NorthwindDB -
i have tried solve question , got this
select c.categoryid, p.productid,p.productname, count(orderid) totalv [order details] od inner join products p on od.productid = p.productid inner join categories c on c.categoryid = p.categoryid inner join (select t.categoryid, min(totalv) minv (select categories.categoryid, products.productid, count(orderid) totalv [order details] join products on [order details].productid = products.productid join categories on categories.categoryid = products.categoryid group products.productid, categories.categoryid) t group t.categoryid) n on c.categoryid = n.categoryid group c.categoryid, p.productid, minv, p.productname having count(orderid) = minv
results :
categoryid | productid | productname | totalv -----------| ----------| ---------------------------| ------ 1 | 67 | laughing lumberjack lager | 10 2 | 15 | genen shouyu | 6 3 | 48 | chocolade | 6 . . . . . . . . . . . .
i'm not sure if way because i'm using twice this:
select categories.categoryid,products.productid, count(orderid) totalv [order details] join products on [order details].productid = products.productid join categories on categories.categoryid = products.categoryid group products.productid, categories.categoryid
so think there better way perform question, help?
Comments
Post a Comment