app/helpers/stats_helpers.rb
#
# Fetches the values needed to be within the 0...100th percentiles
# of the given table/column combination.
#
Fetches the values needed to be within the 0...100th percentiles
of the given table/column combination.
Fetches the values needed to be within the 0 ... 100th percentiles of the given table / column combination .
def percentiles(table, column)
    groups = (0..100).map { |n| n / 100.0 }
    query = <<~SQL.squish
      SELECT unnest(percentile_disc(array[#{groups.join(',')}])
        WITHIN GROUP (ORDER BY #{column} ASC))
      FROM #{table}
      WHERE #{column} IS NOT NULL
    SQL
    ApplicationRecord.connection.execute(query).each_with_index
                     .map { |result, i| ["#{i}%", result["unnest"]] }
                     .to_h
  end
def percentiles ( table , column ) groups = ( 0 .. 100 ) . map { | n | n / 100.0 } query = <<~SQL . squish 
      SELECT unnest(percentile_disc(array[ #{ groups . join ( ',' ) } ])
        WITHIN GROUP (ORDER BY  #{ column }  ASC))
      FROM  #{ table } 
      WHERE  #{ column }  IS NOT NULL
     SQL ApplicationRecord . connection . execute ( query ) . each_with_index . map { | result , i | [ "#{i}%" , result [ "unnest" ] ] } . to_h end
Fetches the values needed to be within the 0...100th percentiles
of the given table/column combination.
Fetches the values needed to be within the 0 ... 100th percentiles of the given table / column combination .
table
column