Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate percentage of group using GROUP BY

I am doing a GROUP BY and COUNT(*) on a dataset, and I would like to calculate the percentage of each group over the total.

For example, in this query, I would like to know how much the count() for each state represents over the total ( select count() from publicdata:samples.natality ):

SELECT state, count(*)
FROM [publicdata:samples.natality]
GROUP by state

There are several ways to do it in SQL, but I haven't found a way to do it in Bigquery, does anyone know?

Thanks!

like image 209
inaki Avatar asked Jun 05 '13 07:06

inaki


People also ask

How do I find the percentage of a group in SQL?

Finding Percentages between two columns is straightforward. You can simply use the column names and the division operator “/” to divide values in one column by another. The result is a list of values that correspond to the result of the division of all the values in the two columns.

How do you find the percentage of a group of data in R?

To calculate percent, we need to divide the counts by the count sums for each sample, and then multiply by 100. This can also be done using the function decostand from the vegan package with method = "total" .

How do you find the percentage of a value in R?

To find the percentage of missing values in each column of an R data frame, we can use colMeans function with is.na function. This will find the mean of missing values in each column. After that we can multiply the output with 100 to get the percentage.


1 Answers

Check ratio_to_report, one of the recently announced window functions:

SELECT state, ratio * 100 AS percent FROM (
 SELECT state, count(*) AS total, RATIO_TO_REPORT(total) OVER() AS ratio
 FROM [publicdata:samples.natality]
 GROUP by state
)

state   percent
AL      1.4201828131159113   
AK      0.23521048665998198  
AZ      1.3332896746620975   
AR      0.7709591206172346   
CA      10.008298605982642
like image 158
Felipe Hoffa Avatar answered Oct 16 '22 05:10

Felipe Hoffa