EQ-1576 Avoid using DISTINCT in SQL