GROUP BY without aggregate function

后端 未结 9 961
孤独总比滥情好
孤独总比滥情好 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:10

    Given this data:

    Col1  Col2  Col3
     A     X     1
     A     Y     2
     A     Y     3
     B     X     0
     B     Y     3
     B     Z     1
    

    This query:

    SELECT Col1, Col2, Col3 FROM data GROUP BY Col1, Col2, Col3
    

    Would result in exactly the same table.

    However, this query:

    SELECT Col1, Col2 FROM data GROUP BY Col1, Col2
    

    Would result in:

    Col1  Col2
     A     X  
     A     Y  
     B     X  
     B     Y  
     B     Z  
    

    Now, a query:

    SELECT Col1, Col2, Col3 FROM data GROUP BY Col1, Col2
    

    Would create a problem: the line with A, Y is the result of grouping the two lines

     A     Y     2
     A     Y     3
    

    So, which value should be in Col3, '2' or '3'?

    Normally you would use a GROUP BY to calculate e.g. a sum:

    SELECT Col1, Col2, SUM(Col3) FROM data GROUP BY Col1, Col2
    

    So in the line, we had a problem with we now get (2+3) = 5.

    Grouping by all your columns in your select is effectively the same as using DISTINCT, and it is preferable to use the DISTINCT keyword word readability in this case.

    So instead of

    SELECT Col1, Col2, Col3 FROM data GROUP BY Col1, Col2, Col3
    

    use

    SELECT DISTINCT Col1, Col2, Col3 FROM data
    
    0 讨论(0)
  • 2020-11-27 12:12

    As an addition

    basically the number of columns have to be equal to the number of columns in the GROUP BY clause

    is not a correct statement.

    • Any attribute which is not a part of GROUP BY clause can not be used for selection
    • Any attribute which is a part of GROUP BY clause can be used for selection but not mandatory.
    0 讨论(0)
  • 2020-11-27 12:21

    You're experiencing a strict requirement of the GROUP BY clause. Every column not in the group-by clause must have a function applied to reduce all records for the matching "group" to a single record (sum, max, min, etc).

    If you list all queried (selected) columns in the GROUP BY clause, you are essentially requesting that duplicate records be excluded from the result set. That gives the same effect as SELECT DISTINCT which also eliminates duplicate rows from the result set.

    0 讨论(0)
提交回复
热议问题