Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Using Group By in Active Records

    • 0
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 235
    Comment on it

    To use a GROUP BY clause to the SQL fired by the finder, you can specify the group method on the find query.

    For example, if you want to find a collection of dates the Orders were created on:

    Order.select("date(created_at) as ordered_date, sum(price) as total_price").group("date(created_at)")

    And this will give you a single Order object for each date where there are orders in the database.

    The sql query for this will be :

    SELECT date(created_at) as ordered_date, sum(price) as total_price
    FROM orders
    GROUP BY date(created_at)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: