Examples to illustrate the functions of database union all

Source: Internet
Author: User


An example is provided to illustrate the function of union all in the database. The present table Aid name1 name21 aa bb2 aa cc3 bb cc4 aa dd ................. www.2cto.com needs to count the number of times each name appears. The expected result is aa 3bb 2cc 2dd 1. The following is a specific step. The first step of reflecting the role of union all through step-by-step analysis is as follows: query the data information of name1 www.2cto.com select name1 name, count (*) num from dd group by name1: name num aa 3 bb 1 ******************************* Step 2: query the data information of name2: select name2 name, count (*) num from dd group by name2: name num bb 1 cc 2 dd 1 ******************************** ** Step 3: combine the first and second steps with the union all results www.2cto.com select name1 name, count (*) num from dd group by name1union allselect name2 name, count (*) num from dd group by name2 query result: name num aa 3bb 1bb 1cc 2dd 1 ******************************** * ******** search again based on Step 3 to obtain the result select name, sum (num) from (select name1 name, count (*) num from dd group by name1 union all select name2 name, count (*) num from dd group by name2) t www.2cto.com group by name; query result: name num aa 3bb 2cc 2 dd 1

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.