如何避免在oracle中重复?

时间:2011-04-14 20:59:39

标签: sql oracle oracle10g

SELECT title, retail, (SELECT AVG(retail)
                 FROM   books) 
FROM   books
WHERE  retail < (SELECT AVG(retail)
                 FROM   books) 

有没有办法缩短其中一个(SELECT AVG(零售)                  从书籍到别名?

2 个答案:

答案 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;