I have this table and I want to add another column to calculate the average of the seconds column.
For example:
my table:
| id | avg |
|---|---|
| 1 | 2.5 |
| 2 | 3.2 |
| 3 | 4.1 |
| 4 | 0.8 |
my desired table:
| id | avg | daily_avg |
|---|---|---|
| 1 | 2.5 | 2.65 |
| 2 | 3.2 | 2.65 |
| 3 | 4.1 | 2.65 |
| 4 | 0.8 | 2.65 |
Is there any simple and short way to do it?
Im using postgreSQL
Thanks
>Solution :
You can use the AVG() window function:
SELECT
id, avg,
AVG(avg) OVER () AS daily_avg
FROM mytable