SELECT DISTINCT on insert query for denesting