Skip to content
Advertisement

Multiple aggregate calculations using Group by

I have a dataset, df1, where I would like to:

  1. Take the average of the TotalB column based upon grouping the TotalB column.
  2. I would then like to take this new column and subtract the free value to obtain the Used value

df1

Desired Outcome

What I am doing:

Any suggestion will be helpful.

Advertisement

Answer

Your query seems pretty close. Mostly you need to fix the GROUP BY:

Note that you cannot re-use the column alias in the same SELECT. You need to repeat the expression.

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