Azmath
Azmath

Reputation: 101

Mysql sql query to format a report

This is my table product_details:

Product_Code | Size | Quantity
-------------+------+-----------
CS01         | 10   | 15
CS01         | 11   | 25
CS01         | 12   | 35
PR01         | 40   | 50
PR01         | 41   | 60

I want a the following format for a report to get the total quantity group by product code (all sizes of product code):

Product_Code | Size       | Quantity
-------------+------------+----------------
CS01         | 10  11  12 | 75
PR01         | 40  41     | 110 

I tried the following query but it does not give the result I want.

SELECT product_no, size, SUM(quantity) 
FROM product_details 
GROUP BY product_no;

Please help me to find the query to format the report.

Upvotes: 0

Views: 88

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You can use group concat

SELECT 
product_no, 
group_concat(size  SEPARATOR ' '),
sum(quantity) 
FROM product_details group by product_no;

Upvotes: 2

Related Questions