?
Solved

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

Posted on 2010-08-27
9
Medium Priority
?
750 Views
Last Modified: 2012-08-08
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)...
0
Comment
Question by:hermesalpha
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 8

Expert Comment

by:PeteEngineer
ID: 33548055
select count(sold_items) as count from table order by count desc
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1000 total points
ID: 33548086
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
 
LVL 1

Expert Comment

by:kollurirk
ID: 33548478
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:kollurirk
ID: 33548483
Replace the table name accordingly
0
 

Author Comment

by:hermesalpha
ID: 33549422
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
 
LVL 2

Assisted Solution

by:IngCharlie
IngCharlie earned 1000 total points
ID: 33550365
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
 
LVL 2

Assisted Solution

by:IngCharlie
IngCharlie earned 1000 total points
ID: 33550392
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
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 1000 total points
ID: 33550881
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
 

Expert Comment

by:GrandadW
ID: 38273779
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question