Skip to content
Browse files

correct invalid GROUP BY query

GROUP BY value must appear in SELECT clause
  • Loading branch information...
1 parent 6a1803a commit cfab51c819111d1dff3e6094e49ed33bc9262305 @amatsuda amatsuda committed Jun 29, 2011
Showing with 2 additions and 2 deletions.
  1. +2 −2 railties/guides/source/active_record_querying.textile
View
4 railties/guides/source/active_record_querying.textile
@@ -466,15 +466,15 @@ To apply a +GROUP BY+ clause to the SQL fired by the finder, you can specify the
For example, if you want to find a collection of the dates orders were created on:
<ruby>
-Order.group("date(created_at)").order("created_at")
+Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)")
</ruby>
And this will give you a single +Order+ object for each date where there are orders in the database.
The SQL that would be executed would be something like this:
<sql>
-SELECT * FROM orders GROUP BY date(created_at) ORDER BY created_at
+SELECT date(created_at) as ordered_date, sum(price) as total_price FROM orders GROUP BY date(created_at)
</sql>
h3. Having

0 comments on commit cfab51c

Please sign in to comment.
Something went wrong with that request. Please try again.