Easily complete summary analysis in data reports

In the previous article, we introduced how to use CASE expressions to add logic processing functions to SQL statements.

In the previous example of row-to-column, we used the SUM function; it is an aggregate function that can summarize and sum data. SQL provides many such functions. In this article, we will learn how to use aggregate functions to implement summary analysis in data reports.

Aggregate function

Summary analysis is the basic function in data reports, such as summary statistics of sales, calculation of average height and standard deviation of students. To this end, SQL provides many aggregate functions with summary functions.

In SQL, Aggregate Function is used to summarize a set of data and return a single analysis result . Common aggregate functions include:

  • COUNT , count the number of rows of query results;
  • AVG , calculate the average value of a set of values;
  • SUM , calculate the sum of a set of values;
  • MAX , calculate the maximum value in a set of data;
  • MIN , calculate the minimum value in a set of data;
  • VAR SAMP , STDDEVSAMP , calculate the variance and standard deviation of a set of data.

Next we demonstrate the role of these functions.

Use the COUNT function to count the number

The COUNT(*) function is used to count the number of rows. The following example counts the number of employees:

SELECT COUNT(*) AS "员工数量"
  FROM employee;

员工数量
------|
    25|
</

Guess you like

Origin blog.csdn.net/horses/article/details/108729106