SQL group by function错误

时间:2014-11-02 07:54:53

标签: mysql oracle group-by aggregate-functions

我在过去3个小时内遇到了这个错误,但我找不到答案。

select store.store_num
       , Count(rental.rental_num)
       , Count(employee.emp_ID)
       , Avg(count(rental.rental_num))
from rental, employee, store
where rental.emp_ID = employee.emp_ID
and store.store_num = employee.store_num
and rental.rent_date >= ’01-JAN-14’
Group by store.store_num

它返回错误行号4,表示

ORA-00937: not a single-group group function.

你能帮帮我吗?

1 个答案:

答案 0 :(得分:0)

您正在加入商店以租赁给员工。因此,您的COUNT()函数将返回相同的值(租赁和员工的产品)。

你没有解释你的作业的逻辑,但让我们猜测:你想要计算每家商店的雇员人数和每家商店的租金数量,以及所有商店的平均租金数量。 / p>

要执行此操作,您需要多个查询。在这里,我定义了两个子查询来获取基本计数,然后我将其加入主查询并用于推导平均值。

with emps as (select store.store_num
                     , count(*) as cnt
              from employee, store
              where store.store_num = employee.store_num
              group by store.store_num)
     , rntls as (select employee.store_num
                     , count(*) as cnt
              from employee, rental
              where rental.emp_ID = employee.emp_ID
              and rental.rent_date >=  date '2014-01-01'
              group by employee.store_num)
select emps.store_num
       , rntl.cnt as rentals_count
       , emps.cnt as employees_count
       , avg(rntl.cnt) as rentals_avg
from emps
     , rntls
where rntls.store_num = emps.store_num
group by emps.store_num
       , rntl.cnt
       , emps.cnt
/

顺便说一句,你应该总是在任何日期字符串中包含世纪:Y2K错误并没有消失,因为它是2014年。