Implementation of pivot in MySQL in SQL Server

Source: Internet
Author: User
Tags mysql in

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.