You are on page 1of 3

Analytic Functions Versus Aggregate Functions

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

Return the same number of rows as the input

Return a single summary value

The groups of rows on which an analytic function operates are defined


by window partitioning and window frame clauses

The groups of rows on which an aggregate function operates


are defined by the SQL GROUP BY clause

Example
This examples illustrate the difference between aggregate functions and their analytic counterpart using table employees defined in the
below sample schema:

CREATE TABLE employees(emp_no INT, dept_no INT);


INSERT INTO employees VALUES(1, 10);
INSERT INTO employees VALUES(2, 30);
INSERT INTO employees VALUES(3, 30);
INSERT INTO employees VALUES(4, 10);
INSERT INTO employees VALUES(5, 30);
INSERT INTO employees VALUES(6, 20);
INSERT INTO employees VALUES(7, 20);
INSERT INTO employees VALUES(8, 20);
INSERT INTO employees VALUES(9, 20);
INSERT INTO employees VALUES(10, 20);
INSERT INTO employees VALUES(11, 20);
COMMIT;

Table employees:

SELECT * FROM employees ORDER BY emp_no;


emp_no | dept_no
--------+--------1|

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

SELECT dept_no, COUNT(*)


AS emp_count
FROM employees

SELECT emp_no, dept_no, COUNT(*) OVER(PARTITION BY dept_no


ORDER BY emp_no)
AS emp_count FROM employees;

GROUP BY dept_no ORDER BY 1;

dept_no | emp_count
---------+-----------

emp_no | 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)

Aggregate function COUNT() returns one


row per department for the number of
employees in that department.

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:

SELECT dept_no, emp_no, COUNT(*)


AS emp_count
FROM employees
GROUP BY dept_no, emp_no ORDER BY 1, 2;
dept_no | emp_no | emp_count
---------+--------+-----------

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

You might also like