How Are Analytic Functions Different From Group or Aggregate Functions?
How Are Analytic Functions Different From Group or Aggregate Functions?
How Are Analytic Functions Different From Group or Aggregate Functions?
COUNT(*) DEPT_COUNT
FROM emp
GROUP BY deptno;
DEPTNO DEPT_COUNT
---------------------- ----------------------
20 5
30 6
2 rows selected
Query-1
Consider the Query-1 and its result. Query-1 returns departments and their employee count. Most
importantly it groups the records into departments in accordance with the GROUP BY clause. As such any
non-"group by" column is not allowed in the select clause.
deptno) DEPT_COUNT
FROM emp
7369 20 5
7566 20 5
7788 20 5
7902 20 5
7876 20 5
7499 30 6
7900 30 6
7844 30 6
7698 30 6
7654 30 6
7521 30 6
11 rows selected.
Query-2
Now consider the analytic function query (Query-2) and its result. Note the repeating values of
DEPT_COUNT column.
This brings out the main difference between aggregate and analytic functions. Though analytic functions
give aggregate result they do not group the result set. They return the group value multiple times with
each record. As such any other non-"group by" column or expression can be present in the select clause,
for example, the column EMPNO in Query-2.
Analytic functions are computed after all joins, WHERE clause, GROUP BY and HAVING are computed on
the query. The main ORDER BY clause of the query operates after the analytic functions. So analytic
functions can only appear in the select list and in the main ORDER BY clause of the query.
In absence of any PARTITION or <window_clause> inside the OVER( ) portion, the function acts on entire
record set returned by the where clause. Note the results of Query-3 and compare it with the result of
aggregate function query Query-4.
FROM emp
ORDER BY 2, 1;
7782 10 8
7839 10 8
7934 10 8
7369 20 8
7566 20 8
7788 20 8
7876 20 8
7902 20 8
Query-3
COUNT(*)
----------
8
2. PRISTOP
What about the amount of work done by Analytics, say if we don't mind rows
to be collapsed and
return all rows with a column for aggregates. Would the analytics do less
work. My guess is they
would do the same amount, because they need to perform aggregation anyway.
vs
???
which would you rather have sitting in your temp? 100 rows, or 10,000
I don't even know why we are having this discussion - it seems so blatantly
obvious that
versus