Update query builder to apply dinstinct function before pagination