SQL modification and data matrice creation