MySQL data at the bottom of the total words the second method of vertical merger 20161103

Source: Internet
Author: User

Last time in the blog http://www.cnblogs.com/Mr-Cxy/p/5923375.html

We used the group BY with Rollup function field custom sort to implement adding the bottom total, the code is very long,

And then we use the vertical UNION ALL function Ifnull sum (if ()) function to achieve vertical merge add total words

SELECTIfnull (F. City, "Total") asCity, f.7 month amount, f.8 month amount, f.9 month amount from (    SELECTE.*     from (        SELECTB. Cities,SUM(IF(b. Month and year=201607, B. Amount,NULL)) asJuly Amount,SUM(IF(b. Month and year=201608, B. Amount,NULL)) asAugust Amount,SUM(IF(b. Month and year=201609, B. Amount,NULL)) asSeptember Amount from (         SELECTCity asCity, Date_format (Order_time, "%Y%M ") asYearsSUM(Pay_money) asAmount fromTest_a03order asaGROUP  byCity,date_format (Order_time, "%Y%m "))  asbGROUP  byB. City) aseUNION  All(        SELECT SUM(IF(1=2,0,NULL)) asCitySUM(IF(d. Month=201607, D. Amount,NULL)) asJuly Amount,SUM(IF(d. Month=201608, D. Amount,NULL)) asAugust Amount,SUM(IF(d. Month=201609, D. Amount,NULL)) asSeptember Amount from (            SELECTCity asCity, Date_format (Order_time, "%Y%M ") asYearsSUM(Pay_money) asAmount fromTest_a03order asCGROUP  byCity,date_format (Order_time, "%Y%m "))  asd)) asF

MySQL data at the bottom of the total words the second method of vertical merger 20161103

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.