Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to calculate proportions in Snowflake

Suppose I have some kind of discreet variable, let's say a string and I want to know the proportion of occurrences of each value of the string. Is there a recommended way to do this in Snowflake ?

like image 386
Vivek Sharma Avatar asked Dec 23 '22 15:12

Vivek Sharma


1 Answers

Snowflake supports RATIO_TO_REPORT:

Returns the ratio of a value within a group to the sum of the values within the group

SELECT C_SALUTATION,
       RATIO_TO_REPORT(COUNT(*)) OVER() AS ratio
FROM "SNOWFLAKE_SAMPLE_DATA"."TPCDS_SF100TCL".CUSTOMER
GROUP BY C_SALUTATION;

enter image description here

like image 190
Lukasz Szozda Avatar answered Jan 14 '23 13:01

Lukasz Szozda