Im trying to archive this SQL to Knext but i dont know how to do it THIS IS THE ORIGINAL QUERY and is fully functional in postgresql using the console THIS IS HOW IM TRYING TO ARCHIVE with knextjs Expected result: Get: Answer Ok. this is the final and correct answer using the same subquery; thanks to Mikael for the