Sobes.tech
Middle

Mis vahe on akna funktsioonil ja agregatsioonifunktsioonil SQL-is?

sobes.tech AI

Vastus AI-lt

The aggregation function in SQL (for example, SUM, AVG, COUNT) calculates a final value for a group of rows and returns one row per group. For example, SUM(salary) will compute the sum of salaries for the entire group.

The window function (window function) also works with groups of rows but returns a result for each row, considering the window context (the set of rows around the current row). For example, SUM(salary) OVER (PARTITION BY department) will return the sum of salaries per department for each row, without reducing the result to a single row.

Example:

SELECT employee_id, department, salary,
       SUM(salary) OVER (PARTITION BY department) AS dept_salary_sum
FROM employees;

Here, for each row, the sum of salaries in the department will be displayed, not one row per department, as with the aggregation function using GROUP BY.