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
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.