This works: last_entry = db().select(db.test.ALL).last() rows = db().select(db.test.ALL, orderby=~db.test.id) next_to_last = rows[1]
That way I have both entries which I can work with. Thank you for your support. On Thursday, January 8, 2015 1:26:42 PM UTC+1, Kiran Subbaraman wrote: > > That's the equivalent of limitby(0,2). See the example in the book: > http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#orderby--groupby--limitby--distinct--having-orderby_on_limitby-left-cache > Yes, the example I gave you was crude, and a bit incorrect - the limitby > takes in two offsets: min and max. Look up the documentation. Also, you > need to ensure that if you want the last two entries, that atleast two > entries exist, and so on. Also, it is not very efficient if you are > frequently going to call a "count" on the database. Lookup some of the > performance tricks for this, particular to your database. > > ________________________________________ > Kiran Subbaramanhttp://subbaraman.wordpress.com/about/ > > On Thu, 08-01-2015 5:14 PM, Timo Bahner wrote: > > Thanks, it works with > > total_entries = db(db.test.id > 0).count() > [next_to_last,last] = db().select(db.test.ALL, limitby=(total_entries-3, 2 > )) > > But crashes with more than 3 entries: > > ValueError: need more than 1 value to unpack > > > However, it works this way (switched "3" with "total_entries"): > > [next_to_last,last] = db().select(db.kpi.ALL, limitby=(total_entries- > *total_entries*, 2)) > > Question: Could this cause a problem (math or otherwise)? > > On Thursday, January 8, 2015 6:55:28 AM UTC+1, Kiran Subbaraman wrote: >> >> Am assuming you want the last two records in a table all the time, and >> they correspond to the variables A, B. >> If you know the number of records in that table, via the count method, or >> a more efficient mechanism, then you can use the limitby statement to get >> the last two records. >> >> total_records = #number of records in the test table - computed via >> count, or some other method >> [A,B] = db().select(db.test.ALL, limitby(total_records - 3, 2) >> >> ________________________________________ >> Kiran Subbaramanhttp://subbaraman.wordpress.com/about/ >> >> On Thu, 08-01-2015 10:56 AM, Timo Bahner wrote: >> >> Hi guys. >> >> Let's assume the following table >> >> db.define_table('test', Field('letter', 'double')) >> >> How can I compare two 'letter' entries if for example >> >> A=1.23 >> B=4.56 >> >> To get the last entry (B) I'll use >> >> last_entry = db().select(db.test.ALL).last() >> >> *But how do I get the entry before the last, in this case letter A?* >> The actual comparing will be done within index.html to check if B is >> greater or equal to A for example >> >> {{if B >= A:}} >> <!-- some html code--> >> {{else:}} >> <!-- some html code--> >> {{pass}} >> >> >> >> -- >> 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]. >> For more options, visit https://groups.google.com/d/optout. >> >> >> -- > 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] <javascript:>. > For more options, visit https://groups.google.com/d/optout. > > > -- 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]. For more options, visit https://groups.google.com/d/optout.

