-2

I want to find out the products that have been ordered the least amount of times without using order by desc or top 1 etc. My query seems to get the min calculation right but the productID and name don't match

SELECT T.ProductID, ProductName, MIN(Total_Quantity)
FROM (SELECT ProductID, SUM(Quantity) as Total_Quantity
      FROM orderdetails
      GROUP BY orderdetails.ProductID) as T
      JOIN products p on T.ProductID = p.ProductID

my code is pasted above and this is the output:

Total Minimum Qty Output

Here is the sum of quantity per product ordered

Total Qty per product

GMB
  • 216,147
  • 25
  • 84
  • 135
Brendan T
  • 1
  • 1

1 Answers1

0

I want to find out the products that have been ordered the least amount of times

This sounds like group by and having:

select p.productid, p.productname, sum(od.total_quantity) as total_quantity
from products p
left join orderdetails od on od.productid = p.productid
group by p.productid
having sum(od.total_quantity) > 3
GMB
  • 216,147
  • 25
  • 84
  • 135