Query editor
-- Constants for the functionWITH params AS (SELECT0.004 AS k, -- Adjust this to fit your distribution needs5000000 AS total_points,5 AS min_points -- Minimum points each user should get),-- Calculate initial points distribution without normalization for each dayinitial_distribution AS (SELECTuser_rank,snapshot_date,fid,fname,display_name,avatar_url,EXP(-k * user_rank) AS initial_points,min_pointsFROMquery_3745451,params),-- Calculate total sum of initial points for each day to find scaling factortotal_initial_points AS (SELECTsnapshot_date,SUM(initial_points) AS sum_initial_pointsFROMinitial_distributionGROUP BY snapshot_date),scaling_factor AS (SELECTsnapshot_date,(total_points - (min_points * 100)) / sum_initial_points AS scale -- Subtract the minimum allocation from total_points before scalingFROMtotal_initial_points,params),-- Apply scaling factor to get the final distribution of points for each day, ensuring a minimum of 5 pointsfinal_distribution AS (SELECTuser_rank,snapshot_date,fid,fname,display_name,avatar_url,GREATEST(ROUND(initial_points * scale), min_points) AS points -- Ensure minimum points using GREATESTFROMinitial_distributionJOINscaling_factor USING (snapshot_date))-- Select final distribution