Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9111183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:26:43+00:00 2026-06-17T03:26:43+00:00

I am trying to write following SQL in rails (via ActiveRecord) and having no

  • 0

I am trying to write following SQL in rails (via ActiveRecord) and having no luck. SQL is following end as such works:

select main_section_id, district_id, sum(answer)
from section_inputs
where year = 2012
and main_section_id= 2 
group by main_section_id, district_id 
order by 3 desc
limit 5

I think that column names are descriptive, in any case following Rails conventions. To sum the problem up, I am trying to get top 5 Districts for specific MainSection, answer column here is integer which represents my score system.

I know question is little too specific (doing my job for me), but I really hit the wall here and if asking for solution is too much some guidance would be great help as well.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:26:44+00:00Added an answer on June 17, 2026 at 3:26 am

    This should work

    SectionInput.select([:main_section_id, :district_id, 'sum(answer) as  total']).where(:year=>2012).where(:main_section_id=>2).group(:main_section_id).group(:district_id).order('3 desc').limit(5)
    

    Else, you can directly include the sql to run

    SectionInput.find_all_by_sql('select main_section_id, district_id,
    sum(answer) from section_inputs where year = 2012 and main_section_id=
    2  group by main_section_id, district_id  order by 3 desc limit 5')
    

    Also, look at the guide to see all Rails 3 querying basics

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write following SQL query using JPA Criteria API SELECT *
I am trying to write equivalent linq code for following query. SELECT A.* FROM
I am trying to write the following query on postgresql: select name, author_id, count(1),
I am trying to write the following SQL. declare @s varchar(max) = ( with
I'm trying to write a sql query to the following (from this site http://sqlzoo.net/1b.htm
I'm trying to write a SQL UNION that works on both MySQL and SQLite.
I'm trying to achieve the following in SQL Server 2005: SELECT (IF EITHER EXISTS)
I'm having a bit of headache with some SQL I'm trying to write. Basically,
I'm trying to write an SQL Trigger with the following approximate form: CREATE TRIGGER
getting errors when trying to write into a SQL database using the 2 following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.