How do I select 10 most sold products from table Sales and select product name from table Product?

How do I select 10 most sold products from table Sales and select product name from table Product?
I want to display product name and number of sold items for each of the ten most sold products.
And order by most sold product first.

I've started like Select Product name, Count(Sold_items) from...
Inner join...
Where (Select Count(10) from Sales)...
LVL 1
hermesalphaAsked:
Who is Participating?
 
cyberkiwiConnect With a Mentor Commented:
select Y.Product_id, Y.SalesCount, P.Product_Name
from
(
select product_id, SalesCount, RN=ROW_NUMBER() over (order by SalesCount desc)
from
(
      select product_id, COUNT(*) SalesCount from sales
) X ) Y
inner join Product p on p.product_id=X.product_id
Where Y.RN<=10 -- top 10


Replace
Sales - with the name of your sales table
Product - your product table
product_id - the foreign_key from sales to product
p.product_id - id of the product table
0
 
PeteEngineerCommented:
select count(sold_items) as count from table order by count desc
0
 
kollurirkCommented:
Following would give you the list in order by the top sold product

select SaleCount, ProdID, ProdName from Product,(
select count(ProductID) SaleCount, ProductID from Sales group by ProductID) x
where x.ProductID=Product.ProductID order by SaleCount desc

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
kollurirkCommented:
Replace the table name accordingly
0
 
hermesalphaAuthor Commented:
Here is my SQL that I've written so far:

Select Productname, Count(Salesdate) AS [Number of sold products]
From Inventory Inner join Sales
On Inventory.ID = Sales.ProductID
Where
(Select Count(ID) From Sales
Where Max(Count(Sales.Salesdate)) = 10
Group by Productname, Salesdate
Order by Count(Salesdate) Desc

The list should display Productname and [Number of sold products], having the  most sold products on the 1st row.
0
 
IngCharlieConnect With a Mentor Commented:
OK two aproaches

Column [Sales].[ProductId] is the key for the [Product].[id] in the [Products] table

Your [Sales] Table is simple

SELECT [ProductName]  FROM [Product]  WHERE [Product].[id] IN (
 SELECT TOP 10  [ProductId]  FROM [Sales] GROUP BY [ProductId] ORDER BY COUNT ([ProductId]) DESC)

Your [Sales] table has an [id] column:

SELECT [ProductName]  FROM [Product]  WHERE [Product].[id] IN (
SELECT TOP 10  [ProductId]  FROM [Sales] GROUP BY [ProductId] ORDER BY COUNT (DISTINCT [Sales].[id]) DESC)

To get the quickest results don't forget to set the indexes:
[Product].[id]
[Sales].[id]
0
 
IngCharlieConnect With a Mentor Commented:
OK two aproaches with the Count

Column [Sales].[ProductId] is the key for the [Product].[id] in the [Products] table

SELECT [ProductName],(SELECT COUNT ([ProductId])  FROM [Sales] WHERE [Sales].[ProductId]=[Product].[id] )  FROM [Product]  WHERE [Product].[id] IN (
 SELECT TOP 10  [ProductId]  FROM [Sales] GROUP BY [ProductId] ORDER BY COUNT ([ProductId]) DESC)



SELECT TOP 10 (SELECT [ProductName]  FROM [Product]  WHERE [Product].[id]=[Sales] .[ProductId]) as 'Product Name',COUNT ( [Sales].[ProductId] ) as Count  FROM [Sales] GROUP BY [ProductId] ORDER BY COUNT ( [Sales].[ProductId] ) DESC)

To get the quickest results don't forget to set the indexes:
[Product].[id]
[Sales].[id]
0
 
cyberkiwiConnect With a Mentor Commented:
select Y.ProductID, Y.[Number of sold products], P.Productname
from
(
      -- the row_number() function gives a ranking to the inner subquery, sorted by
      -- [no. of products sold], so, the most sold=row number 1, next is row #2 etc
      select ProductID, [Number of sold products], RN=ROW_NUMBER() over (order by [Number of sold products] desc)
      from
      (
            -- this creates a subquery of each product id and the number of products sold for that product
            select ProductID, COUNT(Salesdate) [Number of sold products] from sales
      ) X
) Y
      -- join to inventory to retrieve the product name
inner join Inventory p on p.ID=Y.ProductID
Where Y.RN<=10 -- get only row numbers 1 to 10, i.e. the 10 highest sales volume
0
 
GrandadWCommented:
I am experiencing a similar problem but with item purchasing as opposed to sales I am sure this solution with the correct substitutions will help me also, thank you in advance for posting this.
Best regards
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.