Chapter 13 grouped Data group BY, have

Source: Internet
Author: User

Summary data is done on all data in a table or on data that matches a specific where clause, what if you want to return data for a particular situation?
Group. Grouping allows you to divide the data into logical groups so that each group can be clustered.

Create Groups

SELECT Id,count (*) as Prods from products GROUP by ID  

Because group by is used, you do not have to specify each group that you want to calculate and estimate. The system automatically completes, aggregating each group instead of the entire result,
GROUP BY: can contain any number of columns, which makes it possible to nest nested groups, to provide more granular control of data grouping;
If a grouping is nested in a clause, the data is summarized on the last grouping, and each column listed must be a retrieval column or a valid expression.
Each column in a SELECT statement must be given in the GROUP BY clause in addition to the clustered calculation statement;
If there are multiple rows of null values in the Grouping column. They will be divided into a group;
The GROUP by clause must appear after the WHERE clause before ORDER by

Filter Grouping
Having a filter grouping, where filtering rows; having all of the where operators supported, the syntax is the same but the keyword is different

SELECT Id,count (*) as orders from orders the GROUP by ID has a COUNT (*) >=2
SELECT Id,count (*) as orders from the orders WHERE price >=10 the GROUP by ID has COUNT (*) >=2

Grouping and Sorting: The output produced by the order by sort, which can be used by any column (even non-select columns), does not necessarily require
Group by group rows, but the output may not be sorted by grouping; You can use Select columns or expression columns only.
And you must use each of the selection column expressions. If you use a column (or an expression) with a clustered function, you must use the

eg  : SELECT num,sum (Quanity*price) as OrderTotal from orders GROUP by Num have SUM (quanity*price) >=50
  To sort the output by a total order price, you need to add an ORDER BY clause
SELECT Num,sum (Quanity*price) as OrderTotal from orders GROUP by Num have SUM (quanity*price) >=50 ORDER by OrderTotal

SELECT clause sequence

SELECT
From
Group BY to data set rollup
ORDER by LIMIT

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.