31149

Does RAILS have GROUP BY…WITH ROLLUP query?

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")

Recommend

  • list memory usage in ipython and jupyter
  • How to conditionally convert inches to cm in MySQL (or similar conversions during SELECT)?
  • Adding a pivot to a rollup
  • access unkown columns of a table and insert them into a new one
  • Power bi filter column by slicer
  • How to sort and add my database record? [duplicate]
  • How to use aggregate SUM function on result of SUM in INNER JOIN?
  • For Loop or executemany - Python and SQLite3
  • Database Design for One to One relationships
  • ProfileCommon could be not found
  • How to filter a range by first Character in a cell?
  • How to store Button Text of Buttons in a variable? Android Calculator Project
  • sql1 to get names and sql2 to get Impressions, I need to sort names by Impressions
  • Handling null values with wso2 CEP
  • Index Multiple Columns w/ Ruby on Rails
  • How to merge 2 or more columns into one?
  • With one EDMX file use multiple connection strings that relate to multiple databases
  • function for writing out a base 7 word, like binary counter style
  • Select from table that does not exist
  • How to get the date of next specified day of week
  • How Get arguments value using inline assembly in C without Glibc?
  • How to make R's read_csv2() recognise the text characters properly
  • Implementation of State Monad
  • Installing iPhone App to iPhone
  • Abort upload large uploads after reading headers
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Read text file and split every line in MSBuild
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • Display Images one by one with next and previous functionality
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • Data Validation Drop Down Box Arrow Disappearing
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • SQL merge duplicate rows and join values that are different
  • How do you join a server to an Active Directory (domain)?
  • embed rChart in Markdown
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How does Linux kernel interrupt the application?