Gabarito: c)
3.5. Window Functions
A window function performs a calculation across a set of table rows that are somehow related to the current row. This is comparable to the type of calculation that can be done with an aggregate function. But unlike regular aggregate functions, use of a window function does not cause rows to become grouped into a single output row — the rows retain their separate identities. Behind the scenes, the window function is able to access more than just the current row of the query result.
Here is an example that shows how to compare each employee's salary with the average salary in his or her department:
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;
depname | empno | salary | avg
-----------+-------+--------+-----------------------
develop | 11 | 5200 | 5020.0000000000000000
develop | 7 | 4200 | 5020.0000000000000000
develop | 9 | 4500 | 5020.0000000000000000
develop | 8 | 6000 | 5020.0000000000000000
develop | 10 | 5200 | 5020.0000000000000000
personnel | 5 | 3500 | 3700.0000000000000000
personnel | 2 | 3900 | 3700.0000000000000000
sales | 3 | 4800 | 4866.6666666666666667
sales | 1 | 5000 | 4866.6666666666666667
sales | 4 | 4800 | 4866.6666666666666667
(10 rows)
https://www.postgresql.org/docs/9.6/static/tutorial-window.html
SELECT dominio, url, AVG(acessos) OVER(PARTITION BY dominio) AS media_acessos FROM paginas;
dominio | url | media_acessos
www.example.com | /contato.html | 307.0000000000000000
www.example.com | /index.html | 307.0000000000000000
www.example.com | /exemplo.html | 307.0000000000000000
blog.example.com | /index.html | 815.0000000000000000
blog.example.com | /postgres-xc.html | 815.0000000000000000
blog.example.com | /postgresql.html | 815.0000000000000000
evento.example.com | /inscricao.html | 464.5000000000000000
evento.example.com | / | 464.5000000000000000
A cláusula OVER referencia a janela que o PostgreSQL usará para realizar a agregação. A janela foi determinada usando o PARTITION BY, que, (de forma semelhante ao GROUP BY) agrupa as tuplas, fazendo com que todas do mesmo grupo, ou mesma partição, recebam o resultado da agregação. No nosso exemplo, consideramos o campo domínio como uma partição. A função AVG calcula a média para cada janela. Dessa forma, é possível manter as linhas originais apesar dos valores agregados, como mostra a figura: