fix last record queries

(sql is stupid)