Let me make some general points about the scriptures that I hope will help your understanding.
The gospel writers did not necessarily describe everything that Jesus did in chronological order. They grouped teachings according to themes and intent, to convey particular messages.
It is very unlikely that Jesus gave any particular teaching only once. As he traveled around he almost certainly repeated key teachings many times. The gospel writers would not have listed all the times he said a particular thing (why waste ink and paper saying something that was already written). So they may have recorded a saying the first, second or ninety-ninth time he said it. See above.
The summary of the law may not be unique to Jesus. Certainly the two parts are both Old Testament quotes, and the combination may have been known before Jesus.