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
740 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

724 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