【发布时间】:2021-05-27 16:32:30
【问题描述】:
下面是我的 PL/SQL Oracle 代码。它会打印最多雇用员工的年份,并打印该年每月加入的员工数量。
如果我在没有 for 循环部分的情况下执行以下代码,则会产生错误:
不是按功能分组的错误。
我遗漏了一些明显的东西的原因是什么?
declare
v_year number(4);
v_c number(2);
begin
select to_char(hire_date,'yyyy') into v_year
from employees
group by to_char(hire_date,'yyyy')
having count(*) =
( select max( count(*))
from employees
group by to_char(hire_date,'yyyy')); // gets the year where max employees joined
dbms_output.put_line('Year : ' || v_year);
for month in 1 .. 12
loop
select count(*) into v_c
from employees
where to_char(hire_date,'mm') = month and to_char(hire_date,'yyyy') = v_year;
dbms_output.put_line('Month : ' || to_char(month) || ' Employees : ' || to_char(v_c));
end loop;
end;
下面是没有for循环的代码:
declare
v_year number(4);
v_c number(2);
begin
select to_char(hire_date,'yyyy') into v_year
from employees
group by to_char(hire_date,'yyyy')
having count(*) =
( select max( count(*))
from employees
group by to_char(hire_date,'yyyy'));
dbms_output.put_line('Year : ' || v_year);
end;
它会产生以下错误:
错误报告 - ORA-00979:不是 GROUP BY 表达式 ORA-06512:在第 6 行 00979. 00000 - “不是 GROUP BY 表达式”
*原因:
*行动:
【问题讨论】:
-
请不要标记其他 RDBM - SQL Server != Oracle。
-
对不起!更正。这是我第一次发布问题。
-
您确定查询给出了该错误(查询似乎没有问题,除了多年返回的情况,那么将不可能在 SELECT 语句中使用 INTO 子句)?
标签: sql oracle plsql oracle11g