Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Having Clause in Rails

    • 0
    • 1
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 476
    Comment on it

    Hi Friends,
    Previously we discussed about Group By Clause in Rails, Now today lets discuss a related clause of SQL, which is Having. SQL uses having for specifying conditions on GROUP BY clause. Suppose we want to fetch the total views of blogs created at same day where total views in a day is greater than 10. The SQL for that would be:

    SELECT date(created_at) as creation_date, sum(view) as total_views
    FROM blogs
    GROUP BY date(created_at)
    HAVING sum(view) > 10
    

    In Rails this would be written as:

    Blog.select("date(created_at) as creation_date, sum(views) as total_views").group("date(created_at)").having("sum(view) > ?", 10)
    


    Hope you liked this blog. For more like this, Click here.

 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: