I have one requirement to format data in mySql. My raw data in table look like below
For 2nd Product 1 row is missing so, Qty3 and Percentage 3 is 0.
I have tried with below script, but got error
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
' MAX(IF(pd.Qty = ''',
Qty,
''', pd.Percentage, NULL)) AS ',
Percentage
)
) INTO @sql
FROM product;
SET @sql = CONCAT('SELECT pd.GrpId
, pd.ProductId, ', @sql, '
FROM product AS pd
WHERE pd.GrpId = ''1011''
GROUP BY pd.ProductId');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Error I got is 18:50:28 PREPARE stmt FROM @sql Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5.00, MAX(IF(pd.Qty = '20', pd.Percentage, NULL)) AS 12.15, ' at line 2 0.000 sec