fixing distinct bug on sql