ajay kumar
ajay kumar

Reputation: 11

mysql query to get count of products along with quantity

SELECT product,quantity,count(product) AS count 
  FROM order_table WHERE order_id in
   (select order_id from progress) 
group by product

using this query i get product name and count of products. but, i also have quantity column in my table. so, when product gets 2 quantity how to add that quantity along with product count with this above query.

in the above image there are 3 burger products and 2 quantity for one burger product, here i need to show like product : burger and total quantity : 4 later in next line product : pizza and total quantity : 3

Upvotes: 0

Views: 6196

Answers (3)

Maverick Sachin
Maverick Sachin

Reputation: 883

Need some sample data. Input data and expected output. Till then try it out this query --

SELECT Product
    ,SUM(Quantity) AS Total_Quantity
    ,COUNT(Product) AS Product_Count
FROM order_table
WHERE order_id IN (
        SELECT DISTINCT order_id
        FROM Progress
    )
GROUP BY Product

Upvotes: 1

Rohit Kumar
Rohit Kumar

Reputation: 806

SELECT product,sum(quantity)as 'quantity',count(product) AS count FROM order_table WHERE order_id in (select order_id from progress) group by product

Just a use aggrigate function sum on quantity.

Upvotes: 1

Utsav
Utsav

Reputation: 8143

Without sample data it is difficult to guess, but looking at your question, looks like you need something like this

SELECT product
 ,sum(quantity) as total_quantity
 ,count(*) AS count 
  FROM order_table 
 WHERE order_id in
   (select order_id from progress) 
group by product

Upvotes: 0

Related Questions