Fix SQL query for zero lag