improved distinct variables query