Professional Documents
Culture Documents
Like aggregate functions, analytic functions return aggregate results, but analytics do not group the result set. Instead, they return the group
value multiple times with each record, allowing further analysis.
Analytic queries also generally run faster and use fewer resources than aggregate queries.
Analytic functions
Aggregate functions
Example
This examples illustrate the difference between aggregate functions and their analytic counterpart using table employees defined in the
below sample schema:
Table employees:
10
2|
30
3|
30
4|
10
5|
30
6|
20
7|
20
8|
20
9|
20
10 |
20
11 |
20
(11 rows)
Both queries below ask for the number of employees are in each department:
Aggregate query/result
Analytics query/result
dept_no | emp_count
---------+-----------
10 |
1|
10 |
20 |
4|
10 |
30 |
(3 rows)
-----------------------------6|
20 |
7|
20 |
8|
20 |
9|
20 |
10 |
20 |
11 |
20 |
-----------------------------2|
30 |
3|
30 |
5|
30 |
(11 rows)
The analytic function COUNT() returns a count of the number of employees in each department, as well as
which employee is in each department. Within each partition, the results are sorted on the emp_no column,
which is specified in the OVER order by clause.
If you wanted to add the employee number to the above aggregate query, you would add the emp_no column to the GROUP BY clause. For
results, you would get emp_count=1 for each rowunless the data contained employees with the same emp_no value. For example:
10 |
1|
10 |
4|
20 |
6|
20 |
7|
20 |
8|
20 |
9|
20 |
10 |
20 |
11 |
30 |
2|
30 |
3|
30 |
5|
(11 rows)
select uu
from ( select min(a) min_a
, max(a) max_a
from test1
)
connect by level <= max_a - min_a + 1
minus
select a
from test1