3

I want to display greatest selling product by quantity

Product Table
ProductID  ProductName
1          AA   
2          BB
3          CC
[Order Details] Table
OrderID ProductID  Quantity DateOfOrder
1       1            10    SomeDate   
2       1            100     ,,
3       2            15      ,, 
4       1            15      ,,   
5       2            20      ,, 
6       2            30      ,, 
7       1            100     ,,

Expected Output

Product By Quantity  AA

Because sum(quantity)= 225

I used:

select 'Product By Quantity' + ProductName 
from
Products 
where ProductID in
 (select 
       ProductID
  from 
       [Order Details] det 
  where Quantity=
                (
                  select max(SUM(Quantity)) 
                  from [Order Details] od
                  where
                  od.ProductID=det.ProductID
                )
  )  

I got error : "Cannot perform an aggregate function on an expression containing an aggregate or a subquery"

Please explain me why the syntax fails here so that in future i will write appropriate query by knowing the correct syntax.Also give me the correct query.

Thank you everybody in advance.

Edit

I was trying for the following query

SELECT 'Best Selling Product'+ProductName
FROM 
Products
WHERE ProductID =
 (
       SELECT ProductID
       FROM [Order Details]
       GROUP BY ProductID
       HAVING SUM(Quantity) = (
                               SELECT MAX(SQ)
                               FROM (
                                       SELECT SUM(Quantity) as SQ
                                       FROM [Order Details]
                                       GROUP BY ProductID
                                    ) AS OD))
3
  • The error message says it already. You use an aggregate function inside an other aggregate function. Commented Sep 20, 2010 at 19:18
  • Can you show us the data you are dealing with and some desired output. I think your problem lies in what you are trying to do, not the max(sum()) issue. Commented Sep 20, 2010 at 19:20
  • the error is indeed putting sum() inside max(), and your answer to Martijn does not make sense. Try giving an example and we can help. Commented Sep 20, 2010 at 19:30

4 Answers 4

5

I think this is what you're trying to get to:

select top 1 p.product_name, sum(od.quantity) as total_quantity
    from products p
        inner join [order details] od
            on p.productid = od.productid
    group by p.productid, p.product_name
    order by total_quantity desc
Sign up to request clarification or add additional context in comments.

2 Comments

Group by product_name is incorrect. Say, I have a super blaster (id=4) and a super blaster (id=6) but it so happens that the latter isn't selling at all.
@Denis: Thx. I added productid to the group by clause to cover this possibility.
0

Try this:

select 'Product By Quantity' + ProductName  from Products p
join 
(
select top 1 sum(Quantity) sq, od.ProductId 
from [Order Details] od
group by od.ProductId 
order by 1 desc
) bsp on p.productid = bsp.ProductId

bsp stands for Best Selling Product

Comments

0

looks like select max(SUM(Quantity)) is wrong. The maximum of the sum doesn't have any meaning. Did you mean max(Quantity)?

2 Comments

Maximum value of Sum(Quantity) of any product
@Amit - in SQL sum is the total of all the values returned for a column within the query...
0

Try this:

SELECT      TOP 1
            SUM(o.Quantity)
            ,p.ProductName
FROM        [Order Details] AS o
INNER JOIN  [Products] AS p ON p.ProductID = o.ProductID
GROUP BY    p.ProductID
            ,p.ProductName
ORDER BY    SUM(o.Quantity) DESC

1 Comment

Close, but not quite right. You're ordering by the individual quantities rather than the sum. See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.