Group by ALL, groupbyall

Source: Internet
Author: User
Tags repetition

Group by ALL, groupbyall

Group by ALL. The difference between "ALL" and "no" is that after "ALL" is added, ALL groups and result sets are included, and even those groups and result sets whose rows do not meet the search conditions specified by the WHERE clause. Note that if ALL is specified, a null value is returned for the summary column in the group that does not meet the search conditions. Declare @ t table (id int, col char (2) insert @ t select 1, 'A' insert @ t select 1, 'A' insert @ t select 2, 'A' insert @ t select 3, 'A' insert @ t select 3, 'A' insert @ t select 4, 'A' insert @ t select 5, 'A' insert @ t select 5, 'A' insert @ t select 5, 'A' -- 1 select id, COUNT (1) from @ tgroup by id -- 2 select id, COUNT (1) from @ twhere id <3 group by id: /* id ----------- 1 22 13 24 15 3 (5 rows affected) id- ---------- ----------- 1 22 1 (2 rows affected) */what if we want to get the following results? /* Id ----------- 1 22 13 04 05 0 */-- obviously, you will know what I mean at first glance. Some people will definitely join the union or subquery, have you ever thought about how to make it simple? Look at the syntax below: -- 3 select id, COUNT (1) from @ twhere id <3 group by all id results you will have the answer after you run it yourself. This function will be deleted in later versions of Microsoft SQL Server. Avoid using this function in new development work. If you are using this function in future projects, you should modify the applications that are currently using this function. Note the following three points: 1. the CUBE or ROLLUP operator cannot be used with ALL. 2. If there is a WHERE clause in the query to access the remote table, the query does not support group by all. 3. For columns with the FILESTREAM attribute, group by all is not supported.


How to Use group by in SQL statements

Group by is an aggregate function that aggregates queried columns. For example:

C1 C2 C3
A 1 99
A 2 83
A 3 99
B 3 22
SELECT C1, SUM (C2) from t group by C1;
 
Difference between sum and group by in vfp

Sum is the sum.
Group by is a group.
Here are some examples to illustrate:

Select authority, score from JBB
* Read all source data

Select organ, score from JBB group by organ
* [Authority] Each record appears only once-no repetition. [score] = [score] of the last record]

Select organ, sum (score) from JBB
* For one record, [Organ] = [Organ] of the last record, and score = sum of all [scores] In the table

Select organ, sum (score) from JBB group by organ
* [Organ] each organ appears only once-no repetition. [score] = corresponds to the sum of each [Organ's score]
References: you will understand this.

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.