Skip to content
Advertisement

How to average/aggregate the result off a SQL query?

I have a table with many rows each belonging to different users.

I first want to select all rows the belong to one user and then on each row add the average for that a certain column but can’t make it work.

I’m would like the result to display the average on each row for the user but it’s currently just displaying the value for that particular row.

Example :

And the query I’m trying to run:

But I’m just getting the same number that is in the “foo” column for the row as the result for avg_foo where instead I’m trying to get the average of all the “foo” rows displayed on each rows (the would be the same number every time).

Currently I’m getting this result :


Whereas I’m expecting:

I’ve made a fiddle

https://www.db-fiddle.com/f/5tGQkqEnqB8et75eJ7NNWt/0

Advertisement

Answer

Use window functions:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement