Reproduced - Usage principle on the group by

Reprinted: https://blog.csdn.net/u014717572/article/details/80687042

EDITORIAL words: group by using a long time, woke up this morning and suddenly felt a strange group by good, there is always a gluten do not, however, why not be able to select * from Table group by id, why must not be a *, and aggregate function is one column or a column, group by more than one field can be a good understanding of how to do? But finally turn up, simple to write about it, Daniel had a direct skip it.

Text start =========== =========

  First look at Table 1, Table named test:

 

Table 1

  Execute the following SQL statement:

1
2
SELECT  name  FROM  test
GROUP  BY  name

  You should be very easy to know the results of running, yes, that is Table 2 below:

 

Table 2

  However, in order to better understand the "group by" multiple columns "and" aggregate function ", I suggest in the process of thinking in the process of Table 1 to Table 2, the addition of a fictional middle of the table: a virtual table 3. Here to talk about how to think about the implementation of the above SQL statement:

1.FROM test: After the execution of the sentence, and should result in Table 1, as is the original table.

2.FROM test Group BY name: After the execution of the sentence, we can imagine generating the virtual table 3, as shown in the FIG., Such generation processes are: group by name, then the name to find the row with the same name the row values, It merged into one line, as described for aa name value, then <1 aa 2> and <2 aa 3> two lines merged into one line, and all the values ​​of id number value is written to a cell surface Gerry.

 

3. The next step is for the virtual table 3 to perform a Select statement:

(1) If you do select *, then the results should be returned virtual table 3, but the number of content-id and some units Gerry face multiple values, and relational database is based on the relationship of the cell is not It allows multiple values, so you see, perform a select * statement on the error.

(2)我们再看name列,每个单元格只有一个数据,所以我们select name的话,就没有问题了。为什么name列每个单元格只有一个值呢,因为我们就是用name列来group by的。

(3)那么对于id和number里面的单元格有多个数据的情况怎么办呢?答案就是用聚合函数,聚合函数就用来输入多个数据,输出一个数据的。如cout(id),sum(number),而每个聚合函数的输入就是每一个多数据的单元格。

(4)例如我们执行select name,sum(number) from test group by name,那么sum就对虚拟表3的number列的每个单元格进行sum操作,例如对name为aa的那一行的number列执行sum操作,即2+3,返回5,最后执行结果如下:

 (5)group by 多个字段该怎么理解呢:如group by name,number,我们可以把name和number 看成一个整体字段,以他们整体来进行分组的。如下图

(6)接下来就可以配合select和聚合函数进行操作了。如执行select name,sum(id) from test group by name,number,结果如下图:

至此,我已经对我自己对如此简单的问题有如此天马行空的想法所折服,洗洗睡觉。

Guess you like

Origin www.cnblogs.com/qinxiaoqin/p/12100827.html
Recommended