Fixing DISTINCT/LIMIT combination for simple case

This whole method feels somewhat hacky, suggestions welcome. But this
fixes the issue of using DISTINCT and LIMIT in combination (previously
generated invalid SQL)