Got it. For the record I just needed to nest it:
DB[DB[:challenges_images]... ...].select_group(:text).select_append {
avg(:num_selected).as(mean_selected) }
On Friday, 17 February 2017 15:59:28 UTC, Bruce Steedman wrote:
>
> Hi, couldn't find this being asked before, so here goes:
>
> I am creating a migration for a view which needs to 1) group & count rows
> on a given field (:challenge_id). It then needs to 2) average the counts,
> grouped by a *different* field (:text). Result should be columns for
> 'text' and 'mean_selected'.
>
> Part 1) I have achieved with the following code:
> create_view :mean_selected, (DB[:challenges_images].where(selected: true).
> left_join(:challenges, id: :challenge_id).where(solved: true).left_join(:
> challenge_descriptions, text: :desc).select_group(:text,
> :challenge_id).select_append
> { count(challenge_id).as(num_selected) })
>
> This gives the following:
>
>
> *text* *challenge_id* *num_selected*
>
> text integer bigint
> 1 foo 3 3
> 2 bar 1 4
> 3 bar 2 5
> 4 bar 4 6
> I now want to group on :text and average :num_selected. Result should be:
>
> foo 3
> bar 5
>
> If I try simply chaining
>
> .select_group(:text).select_append {avg(num_selected).as(mean_selected) }
>
> To the above expression I get:
>
> ERROR: column "num_selected" does not exist
>
> Sure I'm missing somethign very obvious, but any hep would be greatly
> appreciated.
>
> Can post my schema if required.Thanks.
>
--
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.