I want to generate a report to accumulate the total stock quantity that has been ordered by different branches in a month, and each branch will sum up the quantity by merged (GROUP BY) stock.
i trimmed up my database table for easy understanding as below:
Item Branch Order_Quantity
---------------------------------------
Pencil Branch-A 5
Ruler Branch-D 3
Staple Branch-C 12
Pencil Branch-A 5
Ruler Branch-B 3
Staple Branch-C 2
Pencil Branch-A 10
Ruler Branch-A 6
Staple Branch-D 1
for example, below is draft of expected outcome result:
Item Branch-A Branch-B Branch-C Branch-D
----------------------------------------------------------
Pencil 20 15 32 8
Ruler 12 0 40 10
Staple 4 8 5 0
and so on...
How can I use query to call the above result and assign each sum to their respective branch column?
below is my query:
SELECT `Item`, `Branch`, sum(`Order_Quantity`) FROM `table` GROUP BY `Item`
but when I call and loop the table, the result will show sum quantity to every branches
Item Branch-A Branch-B Branch-C Branch-D
----------------------------------------------------------
Pencil 75 75 75 75
Ruler 62 62 62 62
Staple 17 17 17 17
Hope someone can help for this.
thanks.