$sql = "SELECT * from claims WHERE " . "( bill_process = 1 or bill_process = 5) AND " . "status > 0 AND status < 4"; $res = sqlStatement($sql); $billkeys = array(); while ($row = sqlFetchArray($res)) { $crow = sqlQuery("SELECT count(*) AS count FROM claims WHERE " . "patient_id = '" . $row['patient_id'] . "' AND " . "encounter_id = '" . $row['encounter_id'] . "' AND " . "version > '" . $row['version'] . "'"); if ($crow['count']) continue; $ta = array(); $ta['key'] = $row['patient_id'] . "-" . $row['encounter_id']; $ta['bill_process'] = $row['bill_process']; $ta['format'] = $row['target']; $billkeys[] = $ta; }