On Tuesday, June 18, 2019 at 11:04:16 AM UTC-4, Vlad wrote:
>
> Anthony, it works like a charm, thank you very much!!
>
> (Val's solution with also worked great, but it felt awkward to use the
> whole thing when an alias was created, so emotionally this solution with
> alias feels better :)
>
You can always do:
sum = db.cart_sharing.stats.sum()
And then use sum in multiple places.
> On a side note, for SQLite orderby=['~shares'] works perfect - it indeed
> sorts it out properly. Just in case I've verified it again now.
>
I suppose it works in your case, as you are sorting integers, so the
bitwise negation yields the expected order. However, it won't work in all
cases (e.g., sorting strings). It is simply not the appropriate syntax for
ordering in SQL.
> By the way, Val's solution was orderby=[~db.cart_sharing.stats.count()] -
> with tilda, and it worked even in Postgres. Somehow tilda doesn't work
> specifically with an alias - but without an alias it's just fine on both
> dbs.
>
~db.cart_sharing.stats.sum() is Python code, not SQL. Here, the ~ is part
of the DAL syntax, and the DAL translates that to:
SUM("cart_sharing"."stats") DESC
So, Postgres never sees the tilde, only "DESC".
But in any case, orderby=['shares DESC'] works everywhere flawlessly and
> feels good too, so it's a winner :)
>
Note in this case, 'shares DESC' is just a string, not Python code. The DAL
therefore simply passes that string directly to the database as SQL code.
Also, note there is no reason to put it inside a list.
Anthony
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/web2py/d89ca392-0d5f-4320-81b5-633ca95d51c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.