fixed bug when using SELECT DISTINCT