Accessing some sum

68 views
Skip to first unread message

Dave S

unread,
Aug 2, 2019, 2:59:36 AM8/2/19
to web...@googlegroups.com
In the book, sum of selects is described as:

Similarly, you can use the sum operator to add (sum) the values of a specific field from a group of records. As in the case of count, the result of a sum is retrieved via the storage object:

>>> sum = db.log.severity.sum()
>>> print db().select(sum).first()[sum]
6

But I find that I get a key error when I do this.  It appears that what is actually returned by the select() is

<Row {'_extra': {'SUM("test1t"."item")': 6L}}>
# and to print it I need to do
print results["_extra"].values()[0]


Is this a bug (presumably in DAL), a change from an obsoleted feature, or a mis-reading on my part?


Edit:  Observed on both 2.15.4 on AWS linux, and 2.18.2 on ubuntu 16.04, python 2 in both  cases (2.7.16 and 2.7.12)


/dps

Val K

unread,
Aug 2, 2019, 1:17:27 PM8/2/19
to web2py-users
It is not a bug, as you probably know dal does not support fields aliases and '_extra' is workaround. When do you get an error?

Dave S

unread,
Aug 2, 2019, 6:33:17 PM8/2/19
to web2py-users


On Friday, August 2, 2019 at 10:17:27 AM UTC-7, Val K wrote:
It is not a bug, as you probably know dal does not support fields aliases and '_extra' is workaround. When do you get an error?

Should it be considered a bug in the book?

I'm not quite sure how aliases fit in ... does Postgres consider "sum" in  "select sum(item) from test1t; " an alias? (Ditto sqlite)
I would think "sum" was more like a virtual field.

I get the error in trying to follow the book example, but I did my own table and filled it in using appadmin (2.18.2):

db
.define_table('test1t',
               
Field('item', 'integer'),
               
Field('description', 'string')
               
)

test1t.id test1t.item test1t.description
1           1               something
2           2               buckle my shoe
3           3               fibonacci trey

And I did this because I ran into the issue while running a script to analyze a real database (2.15.4).

/dps


Val K

unread,
Aug 3, 2019, 3:53:02 AM8/3/19
to web2py-users
I just want to say that you can't retrieve sum from row using the string-key 'sum', you should use the object as in the book example. And it would be better if dal supports syntax like db.table.field.sum().as('mysum')

Val K

unread,
Aug 3, 2019, 4:01:21 AM8/3/19
to web2py-users
and of course, if you convert result to json or list/dict you have to use '_extra'

Dave S

unread,
Aug 3, 2019, 4:19:27 AM8/3/19
to web2py-users


On Saturday, August 3, 2019 at 12:53:02 AM UTC-7, Val K wrote:
I just want to say that you can't retrieve sum from row using the string-key 'sum', you should use the object as in the book example. And it would be better if dal supports syntax like db.table.field.sum().as('mysum')

When I put [sum] as the retrieval, I get "key error -- builtin object sum".  When I use ["sum"], I get "key error -- 'sum'". 

I am not converting to JSON or dict,  I just want the value as in the book example.

/dps

Val K

unread,
Aug 3, 2019, 5:47:57 AM8/3/19
to web2py-users
Try db(db.test1).select(sum)

Dave S

unread,
Aug 13, 2019, 5:26:38 AM8/13/19
to web2py-users
On Saturday, August 3, 2019 at 2:47:57 AM UTC-7, Val K wrote:
Try db(db.test1).select(sum)

On hold for the moment, but on another machine I have code that uses the book example, so I must have done something wrong on this machine.

/dps
 

Val K

unread,
Aug 13, 2019, 4:15:42 PM8/13/19
to web2py-users
I saw in some post that implicit query like db() with no args is not supported now

Val K

unread,
Aug 13, 2019, 4:54:44 PM8/13/19
to web2py-users
Reply all
Reply to author
Forward
0 new messages