title: Error grouping by the sum of one column to the general value of the other
body: <p>I have a list of parameters the first are a date range @start_dt and @end_dt one of my fields is mode. if mode = 1, the query is run one way if mode - 2, another, etc. I've got 7 possible versions. </p>
  
  <p>when mode = 6, what I want to say is 
  pull all the columns between the date range list below and where one column pmt_rcvd_amt doesn't equal to the sum of sli_paid_amt. Now I want the sli_paid_amt grouped by customer_no <br><br>
      <code>Where a.create_dt between @start_dt and @end_dt</code> 
      <code>and a.pmt_rcvd_amt &lt;&gt; sum(a.sli_paid_amt)</code></p>
  
  <p>For example: </p>
  
  <pre><code>customer_no sli_due_amt pmt_rcvd_amt`
  85244305    200.00  200.00
  74500386    50.00   219.00
  74500386    219.00  219.00
  74500386    10.00   219.00
  86119821    NULL    NULL
  </code></pre>
  
  <p>I want it to take for customer_no 74500386 <br> it to group them (sum all the sli_due_amt) so 219 + 50. + 10 thus 279 and then compare it to the 219 in pmt_rcvd_amt column. (those values are going to be the same)</p>
  
  <p>my code looks like this ... </p>
  
  <pre><code>select distinct 
          a.id_key ,
          a.customer_no ,
          a.customer_prefix ,
          a.customer_lname 
          ... 
          a.current_child_price ,
          a.current_other_price 
          from LV_CHC_TOURS_RSV_DATA a
          left outer join LT_CHC_TOURS_RSV_LANG b on a.language = b.id 
          left outer join LV_CHC_TOURS_RSV_CS c on a.add_text = c.source_no 
          Where a.create_dt between @start_dt and @end_dt 
          and a.pmt_rcvd_amt &lt;&gt; sum(a.sli_paid_amt)
  
  Error Message: An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
  </code></pre>
  
  <p>I then changed it to try and use having in my code:</p>
  
  <pre><code>from LV_CHC_TOURS_RSV_DATA a
          left outer join LT_CHC_TOURS_RSV_LANG b on a.language = b.id 
          left outer join LV_CHC_TOURS_RSV_CS c on a.add_text = c.source_no 
          Where a.create_dt between @start_dt and @end_dt 
          group by a.customer_no
          having sum(a.sli_paid_amt) &lt;&gt; a.pmt_rcvd_amt
  </code></pre>
  
  <p>and now I get the following error message:</p>
  
  <pre><code>Column 'LV_CHC_TOURS_RSV_DATA.pmt_rcvd_amt' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
  </code></pre>
  
  <p>I know its a bit odd - but I want for a given customer_no to sum all the sli_due_amts and then see if they equal the distinct value of pmt_rcvd_amt. because those distinct values will be the same in each line. </p>
  
  <p>what my desired output is:</p>
  
  <pre><code>customer_no sli_due_amt pmt_rcvd_amt`
  74500386    50.00   219.00
  74500386    219.00  219.00
  74500386    10.00   219.00
  </code></pre>
  
  <p>because: 50 + 219 + 10 doesn't equal to 219 (grouped by customer 74500386)  </p>