SQL按功能分组(列)-现在无法选择该列

|| 我在Oracle Express中使用了HR员工架构,我想选择在特定年份雇用的员工。
  SELECT hire_date, 
         COUNT(*)
    FROM employees empl
GROUP BY SUBSTR(hire_date, -4)
ORDER BY empl.hire_date;
hirer_date列的格式为\“ 1/1/2011 \”,所以我想将它们分组 通过提取最后四个字符。 问题是,我遇到以下错误
ORA-00979: not a GROUP BY expression
00979. 00000 -  \"not a GROUP BY expression\"
*Cause:    
*Action:
Error at Line: 1 Column: 7
这不可能吗?     
已邀请:
如果仅按后四位分组,则无法选择完整的
hire_date
。考虑一下如果有两行,将会发生什么情况:
hire_date
=========
01/01/2001
02/02/2001
在将它们分组时生成的单行中,
hire_date
应该是什么? 所选的每一列都必须是“分组依据”列或“聚合”列。换句话说,请尝试:
select substr(hire_date,-4), count(*)
from employees
group by substr(hire_date,-4)
order by empl.hire_date;
我要提到的是,每行函数在扩展方面非常差。如果要处理大量年份,则应考虑将其分成自己的列。这可能会大大提高性能,但是请不要猜测! 而且,正如其他人在评论中提到的那样,
substr
可能不是最佳解决方案,因为这可能取决于语言环境(例如:日期格式可能为
YYYY-MM-DD
,而with8 not可能不太适合)。 最好使用诸如
to_char(hire_date,\'YYYY\')
或robust10 something之类的东西,它应该更坚固。     
您也可以截断hiratedate列
select trunc(hiredate, \'yyyy\'), count(*) 
from employee
group by trunc(hiredate, \'yyyy\')
    
如果您要按组使用年限雇用他们
select to_char(hiredate,\'yyyy\'),count(*) 
from employee
group by to_char(hiredate,\'yyyy\')
    
您只能在
GROUP BY
查询的
SELECT
部分中使用
GROUP BY
条件或聚合函数(
MIN, MAX, AVG
等)。这有效:
select substr(hire_date,-4), count(*)
from employees empl
group by substr(hire_date,-4)
order by substr(hire_date,-4);
    

要回复问题请先登录注册