title: How to implement sum of a field query in Hibernate?
body: <p>How can we implement the Hibernate sqlprojection in my query? </p>
  
  <p>Here is my query</p>
  
  <pre><code>SELECT sum(total_amount) as total,created_at from order where created_at &lt; DATE_SUB(curdate(), INTERVAL 7 DAY) and doctor_id = 193 GROUP BY created_at
  </code></pre>
  
  <p>I have implement <code>DATE_SUB</code> function using <code>sqlRestriction</code> like this:</p>
  
  <pre><code>String sqlWhere = "created_at &gt; DATE_SUB(curdate(), INTERVAL "+activityGraph+" DAY) AND doctor_id = "+id +" GROUP BY created_at";
  Criteria criteria = Hibernatesession.createCriteria(Order.class);
  criteria.add(Restrictions.sqlRestriction(sqlWhere));
  </code></pre>
  
  <p>But I don't know how I get the sum of a field using Hibernate query.</p>
  
  <p>I found out that <code>setProjection</code> in Hibernate is used to get the sum as we desired but I don't know how to use it. Also here I want to use <code>sqlRestriction</code> to write WHERE condition for <code>date_sub</code> function.<br>
  So I will use <code>setProjection</code> and <code>sqlRestriction</code> in a single query.</p>