filter duplicates directly in the sql query

filter duplicates directly in the sql query.

cc @PVince81