Това не е тествано, създайте цигулка, ако откриете грешки.
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'ifnull(SUM(case when location_code = ''',
location_code ,
''' then quantity end),0) AS `',
location_code , '`'
)
) INTO @sql
FROM
item_details;
SET @sql = CONCAT('SELECT item_number,SUM(quantity) as "total_quantity", ', @sql, '
FROM item_details
GROUP BY item_number');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;