On Jan 17, 10:29 am, rohit <[email protected]> wrote:
> Hi All,
>
> We have several queries with complex case clauses in the select and
> group. Trying to DRY up the code so the case is not repeated. Our
> usual approach to this, which has worked very well in many other
> situations, is to wrap the common code in a function that returns a
> lambda and use it wherever it is needed. In this case we have not been
> able to make it work as well.
>
> Contrived example:
> require 'sequel'
> DB = Sequel.sqlite
>
> def source_case
>   lambda {{({:a__b => 1}) => 'C'}.case('D')}
> end
>
> DB[:a].
>   select{sum(c).as(:sum)}.
>   select_more(&source_case).
>   group(&source_case)
>
> The above works except we would like the case to be aliased in the
> select. Cannot be added to the lambda because group does not accept
> aliases. Cannot be added to &source_case because 'as' is not defined
> on Proc.
>
> One way to work around this:
> select_more(Sequel.virtual_row(&source_case).as(:foo))
>
> Is there a better way?

Dataset#select_group was added to make this easier:

  DB[:a].
    select_group{{({:a__b => 1}) => 'C'}.case('D').as(foo)}.
    select_more{sum(c).as(sum)}

Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to