title: MySQL Use of Having and Where Clause
body: <p>Having difficulty returning the desired results. </p>
  
  <p>Here is my query: </p>
  
  <pre><code>SELECT
      DATABASE()AS INVOICES_Range_$0,
      count(
          DISTINCT invoice_lines.invoice_header_id
      )AS 'Invoice Header Count',
      sum(invoice_lines.accounting_total)AS 'Dollar_Value',
      100 * COUNT(
          DISTINCT invoice_lines.invoice_header_id
      )/(
          SELECT
              COUNT(
                  DISTINCT invoice_lines.invoice_header_id
              )
          FROM
              invoice_lines
          WHERE
              (
                  invoice_lines. STATUS NOT LIKE '%new%'
              )
          AND(
              invoice_lines. STATUS NOT LIKE '%voided%'
          )
      )AS 'Percent of All Invoices',
      COUNT(approvals.approvable_id)/ count(
          DISTINCT invoice_lines.invoice_header_id
      )AS 'AVG_APPROVALS'
  FROM
      invoice_lines
  LEFT JOIN approvals ON invoice_lines.invoice_header_id = approvals.approvable_id
  WHERE
      (
          invoice_lines.accounting_total = 0
      )
  AND(
      invoice_lines. STATUS NOT LIKE '%new%'
  )
  AND(
      invoice_lines. STATUS NOT LIKE '%voided%'
  );
  </code></pre>
  
  <p>This query returns results where any invoice line has a value of $0. </p>
  
  <p>For reference, we may have an invoice where one line is $0 but the other lines total $600. </p>
  
  <p>I am wanting to only include in the above query where the total of all the invoice lines equal $0. </p>
  
  <p>I've tried:</p>
  
  <pre><code>    SELECT
      DATABASE()AS INVOICES_Range_$0,
      count(
          DISTINCT invoice_lines.invoice_header_id
      )AS 'Invoice Header Count',
      sum(invoice_lines.accounting_total)AS 'Dollar_Value',
      100 * COUNT(
          DISTINCT invoice_lines.invoice_header_id
      )/(
          SELECT
              COUNT(
                  DISTINCT invoice_lines.invoice_header_id
              )
          FROM
              invoice_lines
          WHERE
              (
                  invoice_lines. STATUS NOT LIKE '%new%'
              )
          AND(
              invoice_lines. STATUS NOT LIKE '%voided%'
          )
      )AS 'Percent of All Invoices',
      COUNT(approvals.approvable_id)/ count(
          DISTINCT invoice_lines.invoice_header_id
      )AS 'AVG_APPROVALS'
  FROM
      invoice_lines
  LEFT JOIN approvals ON invoice_lines.invoice_header_id = approvals.approvable_id
  WHERE
      (
          invoice_lines. STATUS NOT LIKE '%new%'
      )
  AND(
      invoice_lines. STATUS NOT LIKE '%voided%'
  )
  HAVING
      SUM(
          invoice_lines.accounting_total = 0
      );
  </code></pre>
  
  <p>However, that returns the same results. Also, when modified to </p>
  
  <pre><code>HAVING (SUM(invoice_lines.accounting_total) &lt; 500 )
  </code></pre>
  
  <p>It returns all invoices and the total amount.</p>
  
  <p>Any assistance would be greatly appreciated, as I cannot determine the proper method for limiting my results to those invoice_header_id to only count those invoices where the sum of all lines is equal to 0.</p>