Skip to content

Instantly share code, notes, and snippets.

@ernie
Created June 9, 2011 21:05
Show Gist options
  • Save ernie/1017744 to your computer and use it in GitHub Desktop.
Save ernie/1017744 to your computer and use it in GitHub Desktop.
Attempt #1 at cross-DB select distinct
# So, a SELECT DISTINCT on some DBs requires that all order values are in the select clause.
# I'm hoping there's a better cross-platform way of doing this without branching, and I
# kind of think I must be overlooking something that's already built-in.
# Open to suggestions!
def distinct_relation(relation)
offset, limit = relation.offset_value, relation.limit_value
klass.select('DISTINCT subquery.*').
from(Arel::Nodes::As.new(
Arel::Nodes::Grouping.new(relation.except(:offset, :limit).arel.ast),
Arel.sql('subquery')
))
.offset(offset).limit(limit)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment