Using min(), max(), count() with groupby in SQLFORM.grid

60 views
Skip to first unread message

Vlad

unread,
Dec 23, 2020, 5:52:06 PM12/23/20
to web2py-users
Is there a limitation in the way SQLFORM.grid() can be used with groupby in the sense that functions like min(), max(), count() can't be used? 

the following gives an error: 
fields = [db.auth_user.id, db.my_table.created_on.min(), db.my_table.created_on.max()]
grid = SQLFORM.grid(query, fields=fields, groupby= db.auth_user.id)

the error is: 
<class 'AttributeError'> 'Expression' object has no attribute 'tablename'  

cdbaron

unread,
Dec 24, 2020, 6:29:21 AM12/24/20
to web2py-users
It's a bit of an ugly trick, but I've ever done something like this before.

created_on_min = db.my_table.created_on.min().with_alias(created_on_min)
created_on_max = db.my_table.created_on.max().with_alias(created_on_max)
created_on_min.tablename = 'my_table'
created_on_max.tablename = 'my_table'

fields = [
    db.auth_user.id,
    created_on_min,
    created_on_max
]

Does anyone know of a better way to do it?
Reply all
Reply to author
Forward
0 new messages