Forum: Ruby on Rails Not getting accurate data on multiple field group

Posted by Pradeep Sethi (psethi2000)
on 2012-12-06 12:59
Hi,

I am executing this query:

=====
 @monthly_data = ReviewRating.group("date_format(insertdate, '%Y/%m'),
type").count(:conditions => {:insertdate => '2011-01-01'..'2099-01-01'})

   (12.7ms)  SELECT COUNT(*) AS count_all, date_format(insertdate,
'%Y/%m'), type AS date_format_insertdate_y_m_type FROM
`tc_reviews_rating` WHERE (rev_user_id != 0) AND
(`tc_reviews_rating`.`insertdate` BETWEEN '2011-01-01' AND '2099-01-01')
GROUP BY date_format(insertdate, '%Y/%m'), type

 => {1=>477, 2=>26}
=====

where as if I execute the SQL directly, I get the correct data.


What am I doing incorrect? Thanks in advance for the help.


Pradeep
Posted by Frederick Cheung (Guest)
on 2012-12-07 12:43
(Received via mailing list)
On Thursday, December 6, 2012 11:59:54 AM UTC, Ruby-Forum.com User 
wrote:
>
>
> =====
>  @monthly_data = ReviewRating.group("date_format(insertdate, '%Y/%m'),
> type").count(:conditions => {:insertdate => '2011-01-01'..'2099-01-01'})
>
> When you use the string form of group, rails doesn't look into the string
to see that you are grouping on 2 different things.
I'd try group("date_format(insertdate, '%Y/%m')", "type")

Fred
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.