31149

Question:
<strong>GROUP BY... WITH ROLLUP</strong> is a cool feature in sql.
Does Rails support ROLLUP ?
How can I write the query like,
.group('column1, column2,....')
Answer1:I normally use it like
@rollup = People.select(:occupation, :state, 'COUNT(`state`) as cnt')
.group(:occupation, 'state WITH ROLLUP')
.to_a.map(&:attributes)
then in my view use @rollup.first["state"] etc.
Answer2:You can use the RollUp clause in the group method of ruby
. Let take an example, we have sql query like
SELECT *
FROM Lead
GROUP BY ROLLUP(LeadSource)
In rails we can map this query like:
Lead.group("lead_source with rollup")