I have a database table 'stock_usage' that looks something like this.
id | item_id | store_id | quantity | timestamp
-------------------------------------------------------------
There is an item
table and a store
table. I'm creating a stock usage report in csv with the item in the first column and the rest representing a store each containing the total quantity used as below.
item_name | store_A | store_B | store_C |...
----------------------------------------------------
soap | 3 | 4 | 5 |
My current solution is to retrieve the totals for each item per store as shown below and loop through the results in php to obtain the structure above.
item_name | store | quantity
------------------------------------
soap | store_A | 3
soap | store_B | 4
soap | store_C | 5
Is there a way to obtain this in sql without the additional code?