Oracle SQL group by to_char - not a group by expression

126 views Asked by At

I want to group by dd-mm-yyyy format to show working_hours per employee (person) per day, but I get error message ORA-00979: not a GROUP BY expression, when I remove TO_CHAR from GROUP BY it works fine, but that's not I want as I want to group by days regardless hours, what am I doing wrong here?

    SELECT   papf.person_number emp_id,
         to_char(sh21.start_time,'dd/mm/yyyy') start_time,
         to_char(sh21.stop_time,'dd/mm/yyyy') stop_time,
         SUM(sh21.measure) working_hours
    FROM per_all_people_f papf,
         hwm_tm_rec sh21
         
         

    WHERE ...

    GROUP BY 
         papf.person_number,
         to_char(sh21.start_time,'dd/mm/yyyy'),
         to_char(sh21.stop_time,'dd/mm/yyyy')

    ORDER BY sh21.start_time
1

There are 1 answers

0
MT0 On
ORDER BY sh21.start_time

needs to either be just the column alias defined in the SELECT clause:

ORDER BY start_time

or use the expression in the GROUP BY clause:

ORDER BY to_char(sh21.start_time,'dd/mm/yyyy')

If you use sh21.start_time then the table_alias.column_name syntax refers to the underlying column from the table and you are not selecting/grouping by that.