GROUP BY without aggregate function

后端 未结 9 1008
孤独总比滥情好
孤独总比滥情好 2020-11-27 11:28

I am trying to understand GROUP BY (new to oracle dbms) without aggregate function.
How does it operate?
Here is what i have tried.

EMP table on

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 12:01

    That's how GROUP BY works. It takes several rows and turns them into one row. Because of this, it has to know what to do with all the combined rows where there have different values for some columns (fields). This is why you have two options for every field you want to SELECT : Either include it in the GROUP BY clause, or use it in an aggregate function so the system knows how you want to combine the field.

    For example, let's say you have this table:

    Name | OrderNumber
    ------------------
    John | 1
    John | 2
    

    If you say GROUP BY Name, how will it know which OrderNumber to show in the result? So you either include OrderNumber in group by, which will result in these two rows. Or, you use an aggregate function to show how to handle the OrderNumbers. For example, MAX(OrderNumber), which means the result is John | 2 or SUM(OrderNumber) which means the result is John | 3.

提交回复
热议问题