Skip to content

Instantly share code, notes, and snippets.

@airspeed
Last active February 1, 2016 15:54
Show Gist options
  • Save airspeed/00a11ccdf4f3bcbb8782 to your computer and use it in GitHub Desktop.
Save airspeed/00a11ccdf4f3bcbb8782 to your computer and use it in GitHub Desktop.
def classes_user_auto ( classes, from, to )
results = []
classes.each do | range |
os = Order.where( :state => :printed.to_s, :created_at => from .. to ).group( :user_id ).having( "count(*) >= #{ range.first } AND count(*) <= #{ range.last }" )
n = os.count.count
t = os.sum( :amount_eu ).sum{ | k, v | v }.to_f.round( 2 )
results << [ "#{ from .. to }", "#{ range }", n, t ]
end
results
end
classes = [
0 .. 1,
2 .. 3,
4 .. 6,
7 .. 10,
10 .. 10000
]
from = Date.today.beginning_of_year - 1.year
to = Date.today.beginning_of_year
# Usage:
# classes_user_auto( classes, from, to )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment