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
738 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 250 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 250 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 250 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 250 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

830 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