GROUP BY without aggregate function

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

    Let me give some examples.

    Consider this data.

    CREATE TABLE DATASET ( VAL1 CHAR ( 1 CHAR ),
                       VAL2 VARCHAR2 ( 10 CHAR ),
                       VAL3 NUMBER );
    
    INSERT INTO
          DATASET ( VAL1, VAL2, VAL3 )
    VALUES
          ( 'b', 'b-details', 2 );
    
    INSERT INTO
          DATASET ( VAL1, VAL2, VAL3 )
    VALUES
          ( 'a', 'a-details', 1 );
    
    INSERT INTO
          DATASET ( VAL1, VAL2, VAL3 )
    VALUES
          ( 'c', 'c-details', 3 );
    
    INSERT INTO
          DATASET ( VAL1, VAL2, VAL3 )
    VALUES
          ( 'a', 'dup', 4 );
    
    INSERT INTO
          DATASET ( VAL1, VAL2, VAL3 )
    VALUES
          ( 'c', 'c-details', 5 );
    
    COMMIT;
    

    Whats there in table now

    SELECT * FROM DATASET;
    
    VAL1 VAL2             VAL3
    ---- ---------- ----------
    b    b-details           2
    a    a-details           1
    c    c-details           3
    a    dup                 4
    c    c-details           5
    
    5 rows selected.
    

    --aggregate with group by

    SELECT
          VAL1,
          COUNT ( * )
    FROM
          DATASET A
    GROUP BY
          VAL1;
    
    VAL1   COUNT(*)
    ---- ----------
    b             1
    a             2
    c             2
    
    3 rows selected.
    

    --aggregate with group by multiple columns but select partial column

    SELECT
          VAL1,
          COUNT ( * )
    FROM
          DATASET A
    GROUP BY
          VAL1,
          VAL2;
    
    VAL1  
    ---- 
    b             
    c             
    a             
    a             
    
    4 rows selected.
    

    --No aggregate with group by multiple columns

    SELECT
          VAL1,
          VAL2
    FROM
          DATASET A
    GROUP BY
          VAL1,
          VAL2;
    
        VAL1  
        ---- 
        b    b-details
        c    c-details
        a    dup
        a    a-details
    
        4 rows selected.
    

    --No aggregate with group by multiple columns

    SELECT
          VAL1
    FROM
          DATASET A
    GROUP BY
          VAL1,
          VAL2;
    
        VAL1  
        ---- 
        b
        c
        a
        a
    
        4 rows selected.
    

    You have N columns in select (excluding aggregations), then you should have N or N+x columns

    0 讨论(0)
  • 2020-11-27 11:58

    If you have some column in SELECT clause , how will it select it if there is several rows ? so yes , every column in SELECT clause should be in GROUP BY clause also , you can use aggregate functions in SELECT ...

    you can have column in GROUP BY clause which is not in SELECT clause , but not otherwise

    0 讨论(0)
  • 2020-11-27 11:59

    The only real use case for GROUP BY without aggregation is when you GROUP BY more columns than are selected, in which case the selected columns might be repeated. Otherwise you might as well use a DISTINCT.

    It's worth noting that other RDBMS's do not require that all non-aggregated columns be included in the GROUP BY. For example in PostgreSQL if the primary key columns of a table are included in the GROUP BY then other columns of that table need not be as they are guaranteed to be distinct for every distinct primary key column. I've wished in the past that Oracle did the same as it would have made for more compact SQL in many cases.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 12:06

    I know you said you want to understand group by if you have data like this:

    COL-A  COL-B  COL-C  COL-D
      1      Ac      C1     D1
      2      Bd      C2     D2
      3      Ba      C1     D3
      4      Ab      C1     D4
      5      C       C2     D5
    

    And you want to make the data appear like:

    COL-A  COL-B  COL-C  COL-D
      4      Ab      C1     D4
      1      Ac      C1     D1
      3      Ba      C1     D3
      2      Bd      C2     D2
      5      C       C2     D5
    

    You use:

    select * from table_name
    order by col-c,colb
    

    Because I think this is what you intend to do.

    0 讨论(0)
  • 2020-11-27 12:08

    Use sub query e.g:

    SELECT field1,field2,(SELECT distinct field3 FROM tbl2 WHERE criteria) AS field3
    FROM tbl1 GROUP BY field1,field2
    

    OR

    SELECT DISTINCT field1,field2,(SELECT distinct field3 FROM tbl2 WHERE criteria) AS field3
    FROM tbl1
    
    0 讨论(0)
提交回复
热议问题