Mysql MySQL or PHP Transform rows to two columns dynamically -
i have same scenario have in mysql or php transform rows columns
but have restructure table , output
it's pretty same link above include as_amount.anyone can me?
i want result below as_month_2016...etc in right side of each fa_mont2016
this code:
create definer=`root`@`localhost` procedure `display_annualize_table`() begin set group_concat_max_len=10028; set @sql = null; select group_concat(distinct concat( 'max(if(month = ''', month, ''' , year(date) = ', year(date), ', fs_amount, null)) `', concat('fa_',month), '_', year(date), '`') order date ) @sql tmp_results; if coalesce(@sql,'') != '' set @sql = concat(', ', @sql); end if; set @sql = concat( 'select r.account account, r.region region ', coalesce(@sql,''), 'from tmp_results r left join accounts on r.account_id = a.id group r.account, r.region order r.account_id'); prepare stmt @sql; execute stmt; deallocate prepare stmt; end
need please. thank in advance!
it's works same way fs_amount
, add new columns code generates dynamic columns:
select group_concat(distinct concat( 'max(if(month = ''', month, ''' , year(date) = ', year(date), ', fs_amount, null)) `', concat('fa_',month), '_', year(date), '`, ', 'max(if(month = ''', month, ''' , year(date) = ', year(date), ', as_amount, null)) `', concat('as_',month), '_', year(date), '`' ) order date ) @sql tmp_results;
you should have @ code statements create (e.g. temporary adding select @sql;
), although should pretty straight forward add more columns in case you'll need them.
Comments
Post a Comment