ORACLE SQL查询 - AVG

时间:2015-10-28 00:30:55

标签: sql database oracle relational-database

所以我有以下Oracle SQL数据库表:

create table Customer
(customerID char(25) not null, 
name char(50), 
address char(100), 
dateRegistered DATE, 
noOfDvdHiring int, 
primary key (customerID));

我有以下查询:列出所有客户租用DVD的数量和名称,而不是所有客户目前雇用的平均DVD数量。

我确定这应该涉及嵌套查询,但我不确定从哪里开始?

我试过了:

SELECT customerID, name
FROM Customer
WHERE noOfDvdHire > AVG(noOfDvdHire);

但收到此错误消息:ORA-00934:此处不允许组功能

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT customerID, name
FROM Customer
WHERE noOfDvdHiring > (select AVG(noOfDvdHiring) from customer);

答案 1 :(得分:0)

2种方式......区别在于执行计划

select * from (
  select customer_id, noOfDvdHire, avg(noOfDvdHire) over () avgg from Customer
) where noOfDvdHire > avgg

select customer_id, noOfDvdHire from Customer
  where noOfDvdHire >  (select AVG(noOfDvdHire) from customer);