title: Aging report 30,60,90 above
body: <p>I try to count the no. of bills received between 30,60 and above 90 days.</p>
  
  <p>Here is my T-SQL query:</p>
  
  <pre><code>SELECT 
      costcentreid,
      'Current' = CASE  
                     WHEN DATEDIFF(DAY, BillDate, Getdate()) &lt; 30 
                        THEN COUNT(PVNo) 
                  END, 
      '30_days' = CASE 
                     WHEN DATEDIFF(DAY, BillDate, Getdate()) BETWEEN 30 AND 60  
                        THEN COUNT(PVNo) 
                  END, 
      '60_days' = CASE 
                     WHEN DATEDIFF(DAY, BillDate, Getdate()) BETWEEN 60 AND 90 
                        THEN COUNT(PVNo) 
                  END, 
      '90_plus' = CASE 
                     WHEN DATEDIFF(DAY, BillDate, Getdate()) &gt; 90 
                        THEN COUNT(PVNo) 
                  END
  FROM  
      SPRGMMS..PVRegister
  GROUP BY 
      CostCentreId
  </code></pre>
  
  <p>When I run this, I get an error:</p>
  
  <blockquote>
    <p>Msg 8120, Level 16, State 1, Line 2<br>
    Column '[DB]..PVRegister.BillDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.</p>
  </blockquote>
  
  <p>Kindly help </p>