[web2py] SQL avg()

120 views
Skip to first unread message

Richard Vézina

unread,
May 17, 2012, 2:25:40 PM5/17/12
to web2py-users
Hello,

How I can make an avg() aggregate with web2py?

Richard

Jim Steil

unread,
May 17, 2012, 2:33:38 PM5/17/12
to web...@googlegroups.com
I see sum, min, and max aggregates in the book at section 6.24.4, but no
avg. But, using sum and count (from 6.21.3) you could get there.

-Jim

Richard Vézina

unread,
May 17, 2012, 2:38:16 PM5/17/12
to web...@googlegroups.com
Yeah, that's what I thought just after press send :)

Richard

On Thu, May 17, 2012 at 2:33 PM, Jim Steil <j...@qlf.com> wrote:
I see sum, min, and max aggregates in the book at section 6.24.4, but no avg.  But, using sum and count (from 6.21.3) you could get there.

   -Jim

Richard Vézina

unread,
May 17, 2012, 2:54:02 PM5/17/12
to web...@googlegroups.com
print db().select(db.tab1.f2.sum()/db.tab1.f2.count(), groupby=db.tab1.f1)

Richard

Richard Vézina

unread,
May 17, 2012, 2:56:11 PM5/17/12
to web...@googlegroups.com
Now I am searching a way to sum a list:reference type field with the proper representation of id...

Richard

Jim Steil

unread,
May 17, 2012, 3:09:27 PM5/17/12
to web...@googlegroups.com
I just hacked a quick and dirty solution by inserting the following to dal.py at line 7370:

    def avg(self):
        return Expression(self.db, self.db._adapter.AGGREGATE, self, 'AVG', self.type)

Worked fine on my tests with MySQL, using it just like sum, but using avg instead.

    -Jim

Richard Vézina

unread,
May 17, 2012, 3:27:18 PM5/17/12
to web...@googlegroups.com
Maybe open a ticket and propose a patch!!

Richard

Jim Steil

unread,
May 17, 2012, 3:36:01 PM5/17/12
to web...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages