Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add total average column – postgreSQL

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 :

demo: db<>fiddle

You can use the AVG() window function:

SELECT
    id, avg,
    AVG(avg) OVER () AS daily_avg
FROM mytable
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading