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
734 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
 
LVL 1

Expert Comment

by:kollurirk
ID: 33548483
Replace the table name accordingly
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mySql Syntax 7 32
SQL query 4 29
SQl query 19 14
Trouble connecting to SqlServer database 4 32
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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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 extract information from SQL Server on Database, Connection and Server properties

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now