简体   繁体   中英

Oracle SQL group by to_char - not a group by expression

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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM