Project code in SQL Server:
Select IsNull(Mi line,0) asF_mivalue,IsNull(SMT Line,0) asF_smtvalue,IsNull(Air compressor Station,0) asF_changepre,IsNull(Repair line,0) asF_repairvalue,IsNull(Assembly line,0) asF_assemblyvalue from(SelectF_name,CONVERT(decimal( -,2),sum(F_value)) asF_value fromEnergyvaluewhereF_name like '%mi%' orF_name like '%smt%'orF_name like '%repair%' orF_name like '%assembly%' orF_name= 'Air Compressor Station' Group byf_name) AA pivot (Max(F_value) forF_nameinch(MI line, SMT line, air compressor station, repair line, assembly line)) A
And like the following
PIVOT (MAX (f_zz_basevalue) for F_flag in (F, S)) as S;
# MySQL does not support the pivot function for row and column conversions, so it needs to be converted by SQL statements.
The implementation code is as follows:
CREATE Temporary TABLE IF not EXISTST_tmppivot (SelectF_name,CONVERT(sum(F_value),decimal( -,2)) asF_value fromEnergyvaluewhereF_name like '%mi%' orF_name like '%smt%'orF_name like '%repair%' orF_name like '%assembly%' orF_name= 'Air Compressor Station' Group byf_name);SelectIfnull (MI line,0) asF_mivalue,ifnull (SMT Line,0) asF_smtvalue,ifnull (Air compressor station,0) asF_changepre,ifnull (repair line,0) asF_repairvalue,ifnull (assembly line,0) asF_assemblyvalue from( SELECT MAX(MI line) asMi line,MAX(SMT Line) asSMT Line,MAX(Air compressor station) asAir Compressor station,MAX(Repair line) asRepair line,MAX(Assembly line) asAssembly line from ( SELECT CaseF_name when 'mi line' ThenF_valueEND asmi line, CaseF_name when 'SMT Line' ThenF_valueEND asSMT Line, CaseF_name when 'Air Compressor Station' ThenF_valueEND asAir Compressor station, CaseF_name when 'Repair Line' ThenF_valueEND asRepair Line, CaseF_name when 'assembly line' ThenF_valueEND asassembly line fromT_tmppivot) astmp) asT;DROP Temporary TABLE IF EXISTST_tmppivot;
Implementation of pivot in MySQL in SQL Server