当前位置:网站首页>SQL foundation 9 [grouping data]

SQL foundation 9 [grouping data]

2022-07-04 07:09:00 weixin_ forty-five million nine hundred and two thousand and ei

  1. The data packet
    Using grouping, you can divide data into multiple logical groups , And perform aggregation calculation for each group .

  2. Create grouping
    Grouping is using select Of the statement group by Clause .group by It can be considered as putting the data in a table , According to the value of the grouping field , It is divided into several different sub tables
     Insert picture description here
    The meaning of the above sentence : Counter table tb_order Press order_status Grouping , Return to a different order_status, And their number . give the result as follows .

     Insert picture description here
    *group by Clause can contain any number of columns , Thus, groups can be nested , More detailed data grouping .
    * If in group by Group nested in Clause , The data will be summarized on the last specified group .
    *group by Each column listed in clause must be a number of search columns or valid expressions ., If in select Using expressions in , Must be in group by Clause specifies the same expression .
    * In addition to aggregate calculation statements ,select Each column in the statement must be in group by Given in Clause .
    * If the grouping column null, be null Will be returned as a group .
    *group by Clause must appear in where Before clause ,order by After Clause .

  3. Filter grouping
    Use having Filter groups , All types of where Clause can be used having Instead of , The only difference is ,where Filter line , and having Filter grouping .
     Insert picture description here
     Insert picture description here
    *where Filter before data grouping ,having Filter after data grouping ,where Excluded rows are not included in the group .
    Enter the following example , Filter out receiver_address For the record of Zhongteng building .
     Insert picture description here
     Insert picture description here

  4. Grouping and sorting
    order by Sort the resulting output ,group by Group lines , But the output may not be in the order of grouping .

  5. select Clause order
     Insert picture description here

原网站

版权声明
本文为[weixin_ forty-five million nine hundred and two thousand and ei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141545553525.html