按顺序分组中的列顺序是否重要?

时间:2017-05-18 22:52:36

标签: sql oracle

我正在学习如何将GROUP BY子句与Oracle Database 12c SQL一起使用。 我想知道当你需要在子句中列出多个列时,在GROUP BY子句中列出列的顺序是否会影响最终结果。

为了证明我的意思,我将使用这个样本表,BOOKSHELF:

+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| TITLE                                                                                                PUBLISHER            CATEGORY_NAME        RATING  |
+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| ---------------------------------------------------------------------------------------------------- -------------------- -------------------- ------  |
| To Kill A Mockingbird                                                                                Harper Collins       ADULTFIC             5       |
| Wonderful Life                                                                                       W.W. Norton          ADULTNF              5       |
| Innumeracy                                                                                           Vintage Books        ADULTNF              4       |
| Kierkegaard Anthology                                                                                Princeton Univ Pr    ADULTREF             3       |
| Anne Of Green Gables                                                                                 Grammercy            CHILDRENFIC          3       |
| Good Dog, Carl                                                                                       Little Simon         CHILDRENPIC          1       |
| Letters And Papers From Prison                                                                       Scribner             ADULTNF              4       |
| The Discoverers                                                                                      Random House         ADULTNF              4       |
| The Mismeasure Of Man                                                                                W.W. Norton          ADULTNF              5       |
| Either/Or                                                                                            Penguin              ADULTREF             3       |
| Polar Express                                                                                        Houghton Mifflin     CHILDRENPIC          1       |
| Runaway Bunny                                                                                        Harper Festival      CHILDRENPIC          1       |
| My Ledger                                                                                            Bryla Press          ADULTNF              5       |
| Complete Poems Of John Keats                                                                         Viking               ADULTREF             2       |
| Under The Eye Of The Clock                                                                           Arcade Pub           CHILDRENNF           3       |
| John Adams                                                                                           Simon Schuster       ADULTNF              4       |
| Truman                                                                                               Simon Schuster       ADULTNF              4       |
| Journals Of Lewis And Clark                                                                          Mariner              ADULTNF              4       |
| Gospel                                                                                               Picador              ADULTFIC             4       |
| Emma Who Saved My Life                                                                               St Martin'S Press    ADULTFIC             3       |
| Midnight Magic                                                                                       Scholastic           CHILDRENFIC          1       |
| Harry Potter And The Goblet Of Fire                                                                  Scholastic           CHILDRENFIC          4       |
| Shoeless Joe                                                                                         Mariner              ADULTFIC             3       |
| Box Socials                                                                                          Ballantine           ADULTFIC             3       |
| Trumpet Of The Swan                                                                                  Harper Collins       CHILDRENFIC          3       |
| Charlotte's Web                                                                                      Harper Trophy        CHILDRENFIC          3       |
| West With The Night                                                                                  North Point Press    ADULTNF              3       |
| The Good Book                                                                                        Bard                 ADULTREF             4       |
| Preaching To Head And Heart                                                                          Abingdon Press       ADULTREF             4       |
| The Cost Of Discipleship                                                                             Touchstone           ADULTREF             3       |
| The Shipping News                                                                                    Simon Schuster       ADULTFIC             4       |
+--------------------------------------------------------------------------------------------------------------------------------------------------------+

以下是以GROUP BY子句及其结果为特色的查询:

select publisher, category_name, avg(rating)
from bookshelf
group by category_name, publisher;

PUBLISHER            CATEGORY_NAME        AVG(RATING)
-------------------- -------------------- -----------
Arcade Pub           CHILDRENNF                     3 
Simon Schuster       ADULTNF                        4 
Mariner              ADULTFIC                       3 
Harper Trophy        CHILDRENFIC                    3 
Harper Festival      CHILDRENPIC                    1 
Viking               ADULTREF                       2 
Mariner              ADULTNF                        4 
Houghton Mifflin     CHILDRENPIC                    1 
Scholastic           CHILDRENFIC                  2.5 
Abingdon Press       ADULTREF                       4 
Princeton Univ Pr    ADULTREF                       3 
Random House         ADULTNF                        4 
North Point Press    ADULTNF                        3 
W.W. Norton          ADULTNF                        5 
Vintage Books        ADULTNF                        4 
Grammercy            CHILDRENFIC                    3 
Penguin              ADULTREF                       3 
Picador              ADULTFIC                       4 
Ballantine           ADULTFIC                       3 
Touchstone           ADULTREF                       3 
Harper Collins       ADULTFIC                       5 
Little Simon         CHILDRENPIC                    1 
Scribner             ADULTNF                        4 
Bryla Press          ADULTNF                        5 
St Martin'S Press    ADULTFIC                       3 
Harper Collins       CHILDRENFIC                    3 
Bard                 ADULTREF                       4 
Simon Schuster       ADULTFIC                       4

以下是相同的查询,但切换了GROUP BY子句中列的列的顺序:

select publisher, category_name, avg(rating)
from bookshelf
group by publisher, category_name;

PUBLISHER            CATEGORY_NAME        AVG(RATING)
-------------------- -------------------- -----------
Simon Schuster       ADULTNF                        4 
Harper Trophy        CHILDRENFIC                    3 
Bard                 ADULTREF                       4 
Harper Festival      CHILDRENPIC                    1 
North Point Press    ADULTNF                        3 
Princeton Univ Pr    ADULTREF                       3 
Scribner             ADULTNF                        4 
Bryla Press          ADULTNF                        5 
Abingdon Press       ADULTREF                       4 
Simon Schuster       ADULTFIC                       4 
Grammercy            CHILDRENFIC                    3 
Random House         ADULTNF                        4 
Picador              ADULTFIC                       4 
Harper Collins       ADULTFIC                       5 
W.W. Norton          ADULTNF                        5 
Vintage Books        ADULTNF                        4 
Little Simon         CHILDRENPIC                    1 
Harper Collins       CHILDRENFIC                    3 
Houghton Mifflin     CHILDRENPIC                    1 
Arcade Pub           CHILDRENNF                     3 
Mariner              ADULTNF                        4 
St Martin'S Press    ADULTFIC                       3 
Scholastic           CHILDRENFIC                  2.5 
Mariner              ADULTFIC                       3 
Ballantine           ADULTFIC                       3 
Penguin              ADULTREF                       3 
Touchstone           ADULTREF                       3 
Viking               ADULTREF                       2

除了以不同顺序呈现结果外,结果是相同的。在这种情况下,情况总是这样吗?

1 个答案:

答案 0 :(得分:1)

  • GROUP BY中的列名称顺序无关紧要,结果将相同

注意:结果是按行排序的UN,这意味着每次执行查询时 - 数据库都可能以不同的顺序返回

  • 要在此SET中引入订单,您需要使用ORDER BYORDER BY中的列顺序会对行的顺序产生影响,但与实际结果集不同