我使用以下子句创建了一个表VQ1。
CREATE VIEW VQ1 as
SELECT productid, productname, TO_CHAR(unitprice,'$9,999.99') AS "unitprice"
FROM products
WHERE unitprice > avg(unitprice)
WITH READ ONLY;
我收到错误消息,我无法使用汇总函数AVG()
来查找平均值。
那么我怎样才能为视图找到AVG()
?
答案 0 :(得分:2)
你可以尝试这个伴侣:
CREATE VIEW VQ1 AS
SELECT
productid,
productname,
TO_CHAR(unitprice, '$9,999.99') 'unitprice'
FROM
products
GROUP BY
productid
HAVING
unitprice > AVG(unitprice);
答案 1 :(得分:0)
因为avg
是一个聚合函数,所以除非在group by
子句中指定了其他字段,否则不能在select中使用它。
create view VQ1 as
select productid, productname,
TO_CHAR(unitprice,'$9,999.99') as "unitprice"
from products,
(select avg(unitprice) as avgprice from products)
where unitprice > avgprice
with read only;
答案 2 :(得分:-1)
您可以使用窗口(分析)函数完成此操作:
CREATE VIEW vq1 AS
SELECT productid, productname, unitprice FROM (
SELECT productid, productname, unitprice, AVG(unitprice) OVER () AS avg_unitprice
FROM products
) WHERE unitprice > avg_unitprice
WITH READ ONLY;