Mysql Row and column conversions

Source: Internet
Author: User
Tags stmt

One, the first kind

Original Data Sheet

  

After conversion

  

DROP TABLE IF EXISTStempdynamic;CREATE  Temporary TABLETempdynamic (SELECTP.fspaymentname,sr.fspaymentid,sh.fsshiftname,SUM(Sr.fdrecemoney) asAmttotal fromTbsell assINNER JOINTbsellreceive asSr onS.fssellno=Sr.fssellno andS.fsshopguid=Sr.fsshopguidINNER JOINTbpayment asP onSr.fspaymentid=P.fspaymentid andP.fsshopguid=S.fsshopguid andP.fistatus=1INNER JOINTbsellcheck asSc onSc.fssellno=S.fssellno andS.fsshopguid=Sc.fsshopguidINNER JOINTbshift asSh onSc.fsshiftid=Sh.fsshiftid andSh.fistatus=1WHERES.fibillstatus=3 andS.fsshopguid='C5f0d5c9d8b349f493c180bcc22d9ce0' andS.fsselldate='2016-01-15'GROUP  bySr.fspaymentid,sc.fsshiftid);SET @EE="';Set @str_tmp="'; SELECT   @EE:=CONCAT (@EE,'SUM (IF (fsshiftname=\"', Fsshiftname,'\"',', amttotal,0)) as', Fsshiftname,',') asAa into @str_tmp from ( SELECT DISTINCTFsshiftname fromtempdynamic) AOrder  byLength (AA)descLimit1; SET @QQ=CONCAT ('SELECT ifnull (fspaymentname,\'Total\') as account name,',@str_tmp,'sum (amttotal) as totals from Tempdynamic Group by Fspaymentname with ROLLUP'); PREPAREstmt from @QQ; EXECUTEstmt;deallocate Preparestmt
View Code

Ii. the second type of

Original Data Sheet

  

After conversion

  

1 DROP TABLE IF EXISTStempdynamic;2 CREATE    TABLETempdynamic (3   SELECTSh.fsshiftname,sh.fsshiftid,SUM(S.fdsaleamt) asSales Amount,SUM(S.fddiscountamt) asDiscount Amount,SUM(S.fdserviceamt) asService charge Amount,SUM(S.fdexpamt) asAmount receivable,SUM(S.fdrealamt) asAmount Paid4   fromTbsell ass5 INNER JOINTbsellcheck asSc onSc.fssellno=S.fssellno andS.fsshopguid=Sc.fsshopguid6 INNER JOINTbshift asSh onSc.fsshiftid=Sh.fsshiftid andSh.fistatus=17 WHERES.fibillstatus=38  andS.fsshopguid='C5f0d5c9d8b349f493c180bcc22d9ce0'9  andS.fsselldate='2016-01-15'Ten GROUP  bySc.fsshiftid); One   A DROP TABLE IF EXISTSTempdynamica; - CREATE  Temporary TABLETempdynamica ( - SELECTA.*  from ( the SELECTFsshiftname,'Sales Amount'    asAccount, Sales Amount asAmttotal fromTempdynamicUNION    All  - SELECTFsshiftname,'Discount Amount'    asAccount, discount Amount asAmttotal fromTempdynamicUNION    All  - SELECTFsshiftname,'Service charge Amount'  asAccount, Service charge amount asAmttotal fromTempdynamicUNION    All - SELECTFsshiftname,'Receivable Amount'    asAccount, receivable Amount asAmttotal fromTempdynamicUNION    All + SELECTFsshiftname,'Amount Paid'    asAccount, Amount paid asAmttotal fromtempdynamic - ORDER  byFsshiftname, subjectsDESC) asa); +  A   at SET @EE="'; - Set @str_tmp="'; - SELECT  @EE:=CONCAT (@EE,'SUM (IF (fsshiftname=\"', Fsshiftname,'\"',', Amttotal, 0)) as', Fsshiftname,',') asAa into @str_tmp -  from ( -  SELECT DISTINCTFsshiftname fromtempdynamic -) AOrder  byLength (AA)descLimit1; in SET @QQ=CONCAT ('SELECT ifnull (account, \'Total\') as account name,',@str_tmp,'sum (amttotal) as totals from Tempdynamica Group by account with ROLLUP'); -  PREPAREstmt from @QQ;  to EXECUTEstmt; + deallocate Preparestmt
View Code

Mysql Row and column conversions

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.