change sql for query_by_time()