Mysql
 sql >> Database >  >> RDS >> Mysql

Trasponi le righe alle intestazioni in MYSQL

Ecco un SQL dinamico per ruotare i record,

SET @sql = NULL;

SELECT  GROUP_CONCAT(DISTINCT
        CONCAT('MAX(CASE WHEN date = ''',
               date,
               ''' THEN Value ELSE NULL END) AS ',
               CONCAT('`', date, '`')
               )) INTO @sql
FROM TableName
// WHERE date >= curdate() - interval 3 day  // add condition here
ORDER BY date;



SET @sql = CONCAT('SELECT ', @sql, ' 
                   FROM TableName');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;