9, SQL-GROUP BY statement

SQL-GROUP BY statement

First, the concept of
group by statement: grouping function, group the data according to the rules after by. The packet is messy data set, according to certain rules divided into several small areas, the small region and then summarize process.
Similar to excel in the PivotTable, aggregated data is processed according to some rows / columns.

Two, group by using

1, the polymerization function
group by the polymerization with the use of functions. Conventional polymerization functions are:

  • count () Count
  • sum () sums
  • avg () average
  • max () maximum
  • min () Min

2、group by 语法:
select column_name, aggregate_function(column_name)
from table_name
where colunm_name operator value
group by colum_name

select the column name, aggregate functions (column names) from the table
where the column name = "XX"
Group by Column Name

3, example:
the following table "PRICE":
Here Insert Picture Description
3.1 for each customer to find the total amount of
the SELECT the Customer, SUM (. Price) from. Price
Group by the Customer

Result set similar to the following:
Here Insert Picture Description
If you omit group by, the result set as follows:
Here Insert Picture Description
If a statement is omitted group by, the result set of non-effect we need.
The above select statement specifies two (customer and the sum (price)), "sum (price)" returns a single value ( "price" Comprehensive column), and "customer" returns the value 6 (each corresponding "price" for each row in the table). So we can not get the correct result set.

3.2 group by 一个以上的列
select customer,date,sum(price)from price
group by customer,date

Result set as follows:
Here Insert Picture Description
If the statement becomes:
SELECT Customer, DATE, SUM (ProCE). Price from
Group by Customer

Performing the above statements will be given, because it may result in the presence of a plurality of row values in a column
Here Insert Picture Description
3.3 involving multiple tables
Table 1: price Table
Here Insert Picture Description
Table 2: Department
Here Insert Picture Description
SELECT
(SELECT d.dept_name from Department D WHERE p.dept_no = d.dept_no) as departments,
COUNT (p.customer_no) number as
from the p-. price
Group by p.dept_no

Result set as follows:
Here Insert Picture Description

Where the distinction 3.4 Having
Pre role in the where clause of the query results may be grouped, where conditions do not meet removed, i.e. by filtration before the data packet, where conditions can not contain poly-functions, where conditions were filtered using particular row.
screening effect having clauses group satisfying the condition, i.e., after filtering data packets, the conditions often include poly-function, use the having a specific set of filter conditions, multiple packets may be used for standard packets.

select
(select d.dept_name from department d where p.dept_no = d.dept_no) as 部门,
count (p.customer_no) as 人数
from price p
where p.date > “2019-05-23”
group by p.dept_no
having count(p.costomer_no) >1

Result set as follows:
Here Insert Picture Description

Published 38 original articles · won praise 5 · Views 6040

Guess you like

Origin blog.csdn.net/luluisntlulu/article/details/90514494