ruby on rails - Weird results using distinct() and count() methods on apparently similar code -
the following strange behavior has been spotted rails 4 (rails 4.2.2/ruby 2.2.1) , postgresql 9 (postgresql 9.4.4).
keeping in mind grouping without aggregate expressions calculates set of distinct values in column, following apparently similar lines of code lead strange , weird results:
why does:
item.select( :color, :size ).distinct.count # error return error, while instead:
item.select( :color, :size ).distinct.count( :all ) # wrong item.select( :color, :size ).distinct.count( :color, :size ) # wrong both return wrong result (if there duplicate records same values of :color , :size)?
and why does:
item.select( :color, :size ).group( :color, :size ).count # error return error, while instead:
item.select( :color, :size ).group( :color, :size ).count( :all ).count correctly counts select count(distinct (color, size)) items?
also:
item.group( :color, :size ) # error return error, expected because queries group by clause can include in select list non-aggregated column references if appears in, or functionally dependent upon, group by clause.
but instead rails allows following:
item.group( :color, :size ).count.count that correctly counts select count(distinct (color, size)) items!
Comments
Post a Comment