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 8374509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:56:19+00:00 2026-06-09T14:56:19+00:00

I’m trying to perform calculations, apply conditions for the calculated values and count results

  • 0

I’m trying to perform calculations, apply conditions for the calculated values and count results all in one query. The problem is that I don’t know how to refer to the calculated field from the conditions.

I have Issues.created_on field and I want to find statistics about how many issues were created each week during a range of weeks provided by user.
This how my query looks like in MySql:

mysql>  SELECT status_id as group_id,
YEAR(created_on)*1000+WEEK(created_on,1) as range_value, count(*) as
noOfEntries FROM `issues` WHERE (`issues`.`project_id` IN (1))  GROUP
BY issues.status_id, range_value;

+----------+-------------+-------------+
| group_id | range_value | noOfEntries |
+----------+-------------+-------------+
|        1 |     2012031 |           2 |
|        5 |     2012015 |           1 |
+----------+-------------+-------------+

Here is my code (simplified):

# Range value is dynamic: "YYYYXXX", where YYYY is year and XXX could be week, month or day of year
range_value = "YEAR(created_on)*1000+WEEK(created_on,1)"    
select = "#{range_value} as range_value, count(*) as noOfEntries"
grouping = "range_value"
# other conditions are provided by user, so we just add one here
conditions["range_value"] = range[:min]..range[:max]

rows = Issue.all(:select => select, :conditions => conditions, :readonly => true, :group => grouping)

But I get this error:

ActiveRecord::StatementInvalid (Mysql::Error: Unknown column 'issues.range_value' in 'where clause': SELECT YEAR(created_on)*1000+WEEK(created_on,1) as range_value, 'weeks' as range_type, count(*) as logged_hours, 1 as entries, 'priority_id' as grouping, issues.priority_id as group_id FROM `issues` WHERE (`issues`.`range_value` BETWEEN '2012012' AND '2012031' AND `issues`.`project_id` IN (1))  GROUP BY range_value, issues.priority_id ORDER BY 1 asc, 6 asc):

The code I’m trying to modify is not mine, so I cannot add any new fields or methods to the Issues object. So solution like this will not work for me, I guess.

  • 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-09T14:56:20+00:00Added an answer on June 9, 2026 at 2:56 pm

    I have found an example of code which actually helped:

    ActiveRecord doesn’t have a parameter for including a HAVING clause in
    your database queries and I sometimes find myself needing it. Luckily,
    you can sneak it in on the end of your :group parameter without
    needing to resort to a find_by_sql() call. (I don’t recall ever using
    HAVING without GROUP BY, though there probably are some cases where it
    would make sense to do so.)

    As an example, here’s a query from one of my Rails applications that
    finds all duplicate email addresses in the database:

    duplicates = User.find( :all,
      :select     => "email, COUNT(email) AS duplicate_count",
      :conditions => "email IS NOT NULL AND email != ''",
      :group      => "email HAVING duplicate_count > 1"
    )
    

    So after a slight modification my code works:

    # Range value is dynamic: "YYYYXXX", where YYYY is year and XXX could be
    # week, month or day of year
    range_value = "YEAR(created_on)*1000+WEEK(created_on,1)"    
    select = "#{range_value} as range_value, count(*) as noOfEntries"
    # CHANGED THIS LINE from: grouping = "range_value"
    grouping = " HAVING range_value BETWEEN #{range[:min]} AND #{range[:max]}"
    
    # other conditions are provided by user, so we just add one here
    # REMOVED THIS LINE
    # conditions["range_value"] = range[:min]..range[:max]
    
    rows = Issue.all(:select => select, :conditions => conditions, :readonly => true, :group => grouping)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace

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.