Changed select distinct to work with sqlite