I have data as following(it is just an sample, I have 100+ different types.):
advertiser_id activity_type counts
1 1 2
1 2 5
2 1 3
2 3 9
I would like to change it into
advertiser_id activity_type1 activity_type2 activity_type3
1 2 5 0
2 3 0 9
I wrote code as
SET @sql = NULL;
SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(`activity_type` = ', activity_type, ',counts,NULL)) AS counts', activity_type)) INTO @sql FROM ceciliadb.tbltmp_act
;
SET @sql = CONCAT('SELECT advertiser_id, ', @sql, 'FROM ceciliadb.tbltmp_act GROUP BY advertiser_id');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
But there is error as
mysql> PREPARE stmt FROM @sql;
ERROR 1064 (42000): 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 ceciliadb.tbltmp_act GROUP BY advertiser_id' at line 1
Is there anyone can help?
Best Answer
As for your original question, you need an extra space before
FROM
in the line before yourPREPARE
SUGGESTION
At first glance, the query you need generated looks like this
I would craft it like this:
Since you have 100 types, you nned to wedge those 100 type IDs in the subquery
Since the generated query may be too long, set the GROUP_CONCAT length to 10 MB beforehand
If your columns are not sequential in nature, then use the actual values as headers