Nevermind.  This seems to work:

    subq =
session.query(func.count('*').label('countall')).select_from(ReportableCondition).subquery()

    qry = session.query(ReportableCondition.disease,
                func.count(1).label('number'),
                (cast(100*func.count(1), Float) /
subq)).group_by(ReportableCondition.disease)


On Aug 31, 5:42 pm, nospam <[email protected]> wrote:
> I get an error saying the countall doesn't appear in the groupby, or
> its not an aggregating function.
>
> sqlalchemy.exc.ProgrammingError: (ProgrammingError) column
> "anon_2.countall" must appear in the GROUP BY clause or be used in an
> aggregate function
>
>     session = SqlMapping().createSession()
>
>     subq =
> session.query(func.count('*').label('countall')).select_from(ReportableCond­ition).subquery()
>
>     qry = session.query(ReportableCondition.disease,
>                 func.count(1).label('number'),
>                 ((100*func.count(1)) /
> subq.c.countall)).group_by(ReportableCondition.disease)
>
>     for result in qry:
>         print result
>
> On Aug 29, 5:04 pm, Mike Conley <[email protected]> wrote:
>
>
>
> > This should work
>
> >     subq = sess.query(func.count('*').label('countall')).\
> >         select_from(FilmParticipation).subquery()
>
> >     qry = sess.query(FilmParticipation.PartType,
> >                 func.count(1).label('Amount'),
> >                 ((100*func.count(1)) / subq.c.countall)).\
> >                 group_by(FilmParticipation.PartType)
>
> > --
> > Mike Conley
>
> > On Mon, Aug 29, 2011 at 3:05 PM, nospam <[email protected]> wrote:
> > > I'm trying to construct a query in sqlalchemy similiar to this:
> > > SELECT FilmParticipation.PartType, COUNT(*) AS Amount,
> > >  100*COUNT(*) /(SELECT count(*) FROM FilmParticipation) AS
> > > Percentage_of_Total
> > >  FROM FilmParticipation
> > >  GROUP BY FilmParticipation.PartType;
>
> > > I create a subquery for the nested select, but, I get a
> > > NoSuchColmnError on the percentage part.
>
> > > Any ideas how this could be constructed?
>
> > > Cheers,
> > >  Lars
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "sqlalchemy" 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/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" 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/sqlalchemy?hl=en.

Reply via email to