The cosmic evolution of metallicity from the SDSS fossil record