title: SQL : How can I use sub query in a query with group by section?
body: <p>How can I use sub query in a query with group by section?</p>
  
  <p>I use SQL Server 2008 R2 AND Delphi 2010</p>
  
  <p>I receive this error:</p>
  
  <pre><code>Cannot perform an aggregate function on an expression 
  containing an aggregate or a sub query.
  </code></pre>
  
  <p>Like this query :</p>
  
  <pre><code>select 
      t1.sen, 
      sum(t1.d1)as d1, 
      sum(t1.d2)as d2, 
      sum(t1.d1+t1.d2) as d_sum,
      Round((sum((1000*(t1.d1+t1.d2))/(9500-(
         select sum(t2.t_shab+t2.t_rooz) 
         from tbl1 t2 
         where FCode=81 AND DCode=1 AND t2.sen&lt;=t1.sen
      )))),1) as SSS 
  from 
      tbl1 t1 
  where 
      FCode = 81 
      AND DCode = 1 
  </code></pre>
  
  <p>group by 
      t1.sen</p>