user1846439
user1846439

Reputation: 387

filter a negative number in an array sql

function get_patient_balance($pid) {
  if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
    $brow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
      "pid = ? AND billed = 1 AND activity = 1", array($pid) );
    $srow = sqlQuery("SELECT SUM(fee) AS amount FROM drug_sales WHERE " .
      "pid = ?", array($pid) );
    $drow = sqlQuery("SELECT SUM(pay_amount) AS payments, " .
      "SUM(adj_amount) AS adjustments FROM ar_activity WHERE " .
      "pid = ?", array($pid) );
     return sprintf('%01.2f', $brow['amount'] + $srow['amount']
      - $drow['payments'] - $drow['adjustments']);
  }

Above is the function that I am working with. In this function $brow creates an array from the billing table. In this array there are negative numbers. I want to be able to exclude these numbers from the the SUM(fee) AS amount so that I am adding up the positive number totals only.

Upvotes: 0

Views: 464

Answers (1)

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

I want to be able to exclude these numbers from the the SUM(fee) AS amount so that I am adding up the positive number totals only.

So, you just want to add all fee > 0, right? I think this would do the trick if I got you right:

$brow = sqlQuery("SELECT SUM(fee) AS amount FROM billing WHERE " .
"pid = ? AND billed = 1 AND activity = 1 and fee > 0", array($pid) );

Upvotes: 1

Related Questions