Group by statement usage

Source: Internet
Author: User

-- Group by job_id and then by manager_id
Select job_id, manager_id, sum (salary), count (*) from employees group by job_id, manager_id;

-- Group job_id first, output the result of status grouping, group by job_id, group by job_id, and group the grouped job_id by manager_id to output the result;
-- Output the sum of statistics in the first row (1 = 2 + 3 = 4 + 5 + 6 + 7 + 8 + 9 ). That is, the same result set is output twice according to different grouping conditions.
Select job_id, manager_id, sum (salary), count (*) from employees group by rollup (job_id, manager_id );

-- Group by manager_id first, and output rows. Then, output rows are grouped by job_id. Then, output rows are grouped by job_id and manager_id. That is, in fact, the same result set is grouped and output three times according to three conditions respectively.
Select job_id, manager_id, sum (salary), count (*) from employees group by cube (job_id, manager_id );

-- It can be seen that this is equivalent to the first two groups of the CUBE. The third group (group by job_id and manager_id at the same time) and total rows are missing.
Select job_id, manager_id, sum (salary), count (*) from employees group by grouping sets (job_id, manager_id );
-- In fact, group by grouping sets can also output a total. You only need to add an empty pair of parentheses:
Select job_id, manager_id, sum (salary), count (*) from employees group by grouping sets (job_id, manager_id ,());

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.