Good Day, I'm using a dynamic pivot query to generate a cross tab of product sales by month. There are just over 3K products so that means over 3k columns. When I run the query I get an error. If I limit the number of rows in the original table to under 1586 it runs fine with an output of 16 col, including the 'date' field. I can't figure this out and need some help! Please see the code and error below:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'max(case when PSHDSTK = ''',
PSHDSTK,
''' then MthSales end) AS `',
PSHDSTK,
'`'
)
) INTO @sql
FROM salesbyrow;
SET @sql = CONCAT('SELECT thedate, ', @sql, '
FROM salesbyrow
GROUP BY thedate');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
the error I get is
[Err] 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 'FROM salesbyrow
GROUP BY thedate' at line 2