Aggregate function to aggregate function page 1 |
|||||
Let us consider this exercise: Find the maximum value among the average prices of PCs, counted separately for each manufacturer. Calculation of average cost for makers is not difficult:
However, the standard prohibits the use of a subquery as an argument to aggregate function, i.e. we can not solve the exercise by this way:
In such cases, use a subquery in the FROM clause:
By means of new features of language - window functions - this problem can be solved without a subquery:
Note that window functions admit use of aggregate function as argument. DISTINCT keyword is necessary here because the maximal value, which has been counted up over all set of average values, will be "attributed" to each manufacturer. We can't use aggregate function as a parameter of another aggregate function. This implies impossibility of the solution to the above problem as follows.
But there are no rules without exceptions. As it seems to be unreal, but this query is working in Oracle and returns the required result:
You can assure yourself that it is true by visiting the learn-stage exercises page at sql-ex.ru, selecting Oracle in the DBMS list, and running the query (without checking solution for example). Besides, the solution which uses window function will work in Oracle also. I can suggest that the solution without window function actually uses it implying the OVER() clause implicitly. |