Rails sum query with hstore -
i have got table hstore column. need sumarize of entries of specific hstore key.
one stat record looks this:
id: 2041, tenant_name: "isabela", totals: {"value1"=>"31", "value2"=>"21", "value3"=>"38", "value4"=>"28", "value5"=>"0"}, created_at: tue, 05 may 2015 23:31:01 utc +00:00, updated_at: tue, 05 may 2015 23:31:01 utc +00:00>,
with totals being hstore column. find sum of every records value1 specific day. far got:
string = []; dates.each |date| count = stat.where(created_at: datetime.parse(date).midnight..datetime.parse(date).midnight + 1.day)) string.push(count) end //(dates = array of dates)
does have idea how this?
update got work with:
.sum("(totals->'value1')::integer")
you should :
sums = dates.map |date| stat.where(created_at: datetime.parse(date).midnight..datetime.parse(date).midnight + 1.day)) .sum("totals->'value1'::integer") end
sums
holds summation of value1
of records/per day.
Comments
Post a Comment