GROUP BY and compute by in SQL

Source: Internet
Author: User

GROUP BY

To group a query by using the GROUP BY clause Union aggregate function:

Group BY is used to group the result set in pairs, and each group of segments is summarized.

Syntax format: GROUP BY column name [having conditional expression]

It is grouped by the specified columns, the same records in that column are made into a group, each group is evaluated, and having the grouped records are filtered, where clauses are filtered before they are summarized.

Note: The columns displayed in the SELECT statement are the columns that participate in the calculation and the columns in the GROUP BY clause.

Example: Select Egg_liang, Count (Egg_jia) as number, sum = SUM (Egg_jia)

(Columns to display)

From egg

GROUP BY Egg_liang-----> Columns to group (must be the same as above)

Having sum (Egg_jia) > 250

COMPUTE BY

Use the COMPUTE BY clause: It is used to group all of the data in the result set, and the difference between it and group by IS that compute by not only displays summary data, but also shows the record details of participating totals.

Syntax format: COMPUTE aggregate function [by column name]------> "aggregate function" can write multiple

Note: You must sort the summarized columns before you can summarize them with compute by.

Cases

SELECT * from Qian ORDER by Biao_jia Compute sum (biao_liang) by Biao_jia

(Columns to sort) (columns to summarize) (must be the same as the columns to be sorted)

GROUP BY and compute by in SQL

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.