SELECT title, retail, (SELECT AVG(retail)
FROM books)
FROM books
WHERE retail < (SELECT AVG(retail)
FROM books)
有没有办法缩短其中一个(SELECT AVG(零售) 从书籍到别名?
答案 0 :(得分:4)
试试这个:
SELECT *
FROM (
SELECT title, retail, AVG(retail) OVER() avg_retail
FROM books
)
WHERE retail < avg_retail
或使用WITH:
WITH dat AS
(
SELECT title, retail, AVG(retail) OVER() avg_retail
FROM books
)
SELECT *
FROM dat
WHERE retail < avg_retail
答案 1 :(得分:2)
你可以用WITH
子句重构,也许是这样的:
With avg_retail as
(SELECT AVG(retail) the_avg
FROM books)
SELECT title, retail, avg_retail.the_avg
from books, avg_retail
where books.retail < avg_retail.the_avg;