Hello supposed to have a temporary table that having resultset like this:
And the month column is changing dynamically based on user input. Now I want my month column values (months) to become columns and under it was the amount and everything will be group by account_id.
I've searched every answers for this but I still can't get it. Here's my code but it is not returning accurately and I'm still lost here:
CREATE DEFINER=`root`@`localhost` PROCEDURE `test`()
BEGIN
SET group_concat_max_len=2048;
SELECT CONCAT('
SELECT account_id, month ',
GROUP_CONCAT('
GROUP_CONCAT(IF(amount=',QUOTE(amount),',value,NULL))
AS `',REPLACE(amount,'`','``'),'`'
), '
FROM tmp_results
GROUP BY account_id
')
INTO @sql
FROM (
SELECT account_id, month FROM tmp_results
) t;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
Can someone help me step by step that I could understand it? PHP laravel step by step also are welcome. I'm a newbie developer. Thank you!