On Tuesday, August 28, 2018 at 12:04:13 PM UTC-7, [email protected] wrote:
>
> I want to create a query that will generate histogram buckets:
>
> ds = Email.dataset.select(Sequel.function(:count).*.as(:total))
> start = 0
> interval = 1
>
> while start < 10 do
> end_of_bucket = start + interval
> bucket_count = Sequel.function(:count).*.filter do
> (value_column >= start) & (value_column < end_of_bucket)
> end
> ds = ds.select_more(bucket_count)
> start = end_of_bucket
> end
>
> ds.all
>
> But `start` and `end_of_bucket` all get evaluated at the end, so each
> column selection is `COUNT(*) FILTER WHERE (value_column >= 10 AND value <
> 10)`.
>
> How can I force immediately evaluation of the block passed to `filter` to
> get the desired result?
>
You could force early evaluation using Sequel.expr:
bucket_count = Sequel.function(:count).*.filter(Sequel.expr do
(value_column >= start) & (value_column < end_of_bucket)
end)
But you don't really need to use a block for that expression, you can use a
range. Here's an alternative approach:
interval = 1
function = Sequel.function(:count).*
buckets = (0...10).map do |start|
function.filter(:value_column=>start...(start+interval)).as("bucket#{start}")
end
ds = Email.select(Sequel.function(:count).*.as(:total), *buckets)
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.