问题
GROUP BY... WITH ROLLUP is a cool feature in sql.
Does Rails support ROLLUP ?
How can I write the query like,
.group('column1, column2,....')
回答1:
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.
回答2:
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")
来源:https://stackoverflow.com/questions/33007596/does-rails-have-group-by-with-rollup-query