SQL Server compute by clause usage example

Source: Internet
Author: User

1. Related Concepts

If you need to save the query results and display the statistical results under the query results, you can use the compute by clause.

The compute clause is used to generate a total and list it as an additional summary at the end of the current result set. When used together with the by clause, the compute clause generates a control interruption and Classification summary in the result set. You can specify compute by and compute in the same query.

The syntax format is as follows:

[Compute

{

{AVG | count | max | min | STDev | stdevp | Var | varp | sum (expression )}[,... N]

[By expressin [,...]

}]

Aggregate functions used in the compute clause

Note: The field alias cannot be used for Aggregate functions.

 

Ii. instance description

1. Preparations

If object_id ('student ', 'table') is not nullddrop table studentgocreate table student (ID int identity () not null, sex varchar (4) not null, sclass varchar (10), score int, constraint zhujian primary key (ID), constraint scorecheck check (score> 0 and score <= 10) insert into student values ('male ', 1, 2) insert into student values ('female ', 2, 4) insert into student values ('female', 2, 6) insert into student values ('female ', 1, 2) insert into student values ('male',) insert into student values ('male)

2. Group by clause

-- Group words select sex, sclass, score, sum (score) as total score from studentgroup by sex, sclass, scoreorder by sex
Result 3: Use the compute by clause.
-- Comlpute by statement select sex, sclass, score, sum (score) as total score from studentgroup by sex, sclass, scoreorder by sexcompute sum (score), AVG (score ), max (score), min (score) by sex

The result is as follows:

Finished.
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.