ORA-00979 not a group by expression

后端 未结 8 1743
梦谈多话
梦谈多话 2020-11-21 04:33

I am getting ORA-00979 with the following query:

SELECT cr.review_sk, cr.cs_sk, cr.full_name,
tolist(to_char(cf.fact_date, \'mm/dd/yyyy\')) \"appt\",
cs.cs_i         


        
相关标签:
8条回答
  • 2020-11-21 04:41

    You must put all columns of the SELECT in the GROUP BY or use functions on them which compress the results to a single value (like MIN, MAX or SUM).

    A simple example to understand why this happens: Imagine you have a database like this:

    FOO BAR
    0   A
    0   B
    

    and you run SELECT * FROM table GROUP BY foo. This means the database must return a single row as result with the first column 0 to fulfill the GROUP BY but there are now two values of bar to chose from. Which result would you expect - A or B? Or should the database return more than one row, violating the contract of GROUP BY?

    0 讨论(0)
  • 2020-11-21 04:45

    You should do the following:

    SELECT cr.review_sk, 
           cr.cs_sk, 
           cr.full_name,
           tolist(to_char(cf.fact_date, 'mm/dd/yyyy')) "appt",
           cs.cs_id, 
           cr.tracking_number
    from review cr, cs, fact cf
    where cr.cs_sk = cs.cs_sk
           and UPPER(cs.cs_id) like '%' || UPPER(i_cs_id) || '%'
           and row_delete_date_time is null
           and cr.review_sk = cf.review_wk (+)
           and cr.fact_type_code (+) = 183050
    GROUP BY cr.review_sk, cr.cs_sk, cf.fact_date, cr.tracking_number, cs.cs_id, cr.full_name
    ORDER BY cs.cs_id, cr.full_name;
    
    0 讨论(0)
  • 2020-11-21 04:49

    Include in the GROUP BY clause all SELECT expressions that are not group function arguments.

    0 讨论(0)
  • 2020-11-21 04:53

    Too bad Oracle has limitations like these. Sure, the result for a column not in the GROUP BY would be random, but sometimes you want that. Silly Oracle, you can do this in MySQL/MSSQL.

    BUT there is a work around for Oracle:

    While the following line does not work

    SELECT unique_id_col, COUNT(1) AS cnt FROM yourTable GROUP BY col_A;
    

    You can trick Oracle with some 0's like the following, to keep your column in scope, but not group by it (assuming these are numbers, otherwise use CONCAT)

    SELECT MAX(unique_id_col) AS unique_id_col, COUNT(1) AS cnt 
    FROM yourTable GROUP BY col_A, (unique_id_col*0 + col_A);
    
    0 讨论(0)
  • 2020-11-21 05:02

    In addition to the other answers, this error can result if there's an inconsistency in an order by clause. For instance:

    select 
        substr(year_month, 1, 4)
        ,count(*) as tot
    from
        schema.tbl
    group by
        substr(year_month, 1, 4)
    order by
        year_month
    
    0 讨论(0)
  • 2020-11-21 05:03

    Same error also come when UPPER or LOWER keyword not used in both place in select expression and group by expression .

    Wrong :-

    select a , count(*) from my_table group by UPPER(a) .
    

    Right :-

    select UPPER(a) , count(*) from my_table group by UPPER(a) .
    
    0 讨论(0)
提交回复
热议问题