Skip to content
Advertisement

SQL – get an AVG from a COUNT

how you doing?

I’m trying to get the average from a count. The metric is a string, so I get an error. I’ve tried some ways, but I can’t. Thanks for your help

  • This is the code

This is what I’ve unsuccessfully tried

  • AVG (newsletter) as percentage
  • AVG (CAST (newsletter as INT64)) as percentage
  • COUNT(newsletter) / SUM(newsletter)

I would like to get a table like this

Advertisement

Answer

To get the ratio of the current row to the whole table…

  • you already have the value for each individual row
  • use window functions to get the total for the whole table
  • then divide the two

(With a “window” of () to represent the whole table)

In your case, x is COUNT(newsletter) which gives…

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