Group by usage

Source: Internet
Author: User

Aggregate functions
Sum function--sum ()

Count function--count ()

Max/min value function-max ()/min ()

Mean value function--avg ()

—————————————————————————————————————————

Group BY is a grouped query, and general group by is used in conjunction with aggregate functions, you can think about

You used group BY press ITEM. Itemnum This field is grouped, the other field content is different, become a one-to-many and change how to show it, such as the following

A B
1 ABC
1 BCD
1 ASDFG

Select A,b from table GROUP by A
What do you mean by finding out this way,

A B
Abc
1 BCD
Asdfg

The right 3 how to become one, so you need to use aggregate functions, such as

Select A,count (B) Quantity from table group by A
So the result is
A Quantity
1 3

——————————————————————————————————————

Store_information table

Store_name Sales Date

Los Angeles $1500 jan-05-1999

San Diego $ jan-07-1999

Los Angeles $300 jan-08-1999

Boston $700 jan-08-1999

We'll break into
SELECT Store_name, SUM (Sales) from Store_information GROUP by Store_name

Results:

Store_name SUM (Sales)

Los Angeles $1800

San Diego $

Boston $700

————————————————————————————————————————

Group by has a principle that all columns following the Select Do not have columns that use aggregate functions and must appear after group by.

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.