0

I'd like to get an average per type then join the aggregated table as a new column in the original table. Here's a visualization and code of what I'm attempting to do:

-- The original table --

ID | Cnt | Type
 1    5     A
 1    6     A   
 2    4     B

-- New Table -- 

ID | Cnt | Type | Avg
 1    5     A     5.5
 1    6     A     5.5
 2    4     B     4.0

The code I have written thus far is the following:

select AVG(Cnt)
  from old
group by(type)
right join on old

But, obviously it's not correct since a syntax error is raised. What would be the fix for this? I apologize in advance if my question is similar to an already existing one.

1 Answer 1

1

Use window functions:

select o.*, avg(cnt) over (partition by type)  
from old o;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.