first working version of select distinct
Implementing a draft version of DISTINCT ON with a fallback