How To Select Only The Latest Rows from a Table
For a Table structure as below:
Table(
PrimaryKey,
FieldName1,
FieldName2,
Time)
FieldName1 and FieldName2 are not unique.
Goal: To select only the latest row for each FieldName2
Items = db(db.Table.FieldName1 != Value1).select(orderby=~db.Table.Time, limitby=limitby, distinct=db.Table.FieldName2)
How to write the above statement so that it selects All the rows from the Table such that:
– For any given condition which here is: db.Table.FieldName1 != Value1 (works)
– Select All Rows from Table (works)
– Order those rows by Time field (works)
– limit selection by limitby number (works)
– And Selects only the Latest row for FieldName2 : Latest measured by Time ( distinct=db.Table.FieldName2 DOES NOT WORK)
Thank you, very much for reading. Only the part of selecting the distinct last element for FieldName2 does not work and I cannot figure it out. I hope someone can help.
—