SELECT f.id, f.date, f.pid, CONCAT(w.lname, ', ', w.fname) AS provider_id, f.encounter, f.last_level_billed, f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, f.in_collection, p.fname, p.mname, p.lname, p.street, p.city, p.state, p.postal_code, p.phone_home, p.ss, p.billing_note, p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, ( SELECT bill_date FROM billing AS b WHERE b.pid = f.pid AND b.encounter = f.encounter AND b.activity = 1 AND b.code_type != 'COPAY' LIMIT 1) AS bill_date, ( SELECT SUM(b.fee) FROM billing AS b WHERE b.pid = f.pid AND b.encounter = f.encounter AND b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, ( SELECT SUM(b.fee) FROM billing AS b WHERE b.pid = f.pid AND b.encounter = f.encounter AND b.activity = 1 AND b.code_type = 'COPAY' ) AS copays, ( SELECT SUM(s.fee) FROM drug_sales AS s WHERE s.pid = f.pid AND s.encounter = f.encounter ) AS sales, ( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE a.pid = f.pid AND a.encounter = f.encounter AND a.deleted IS NULL) AS payments, ( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE a.pid = f.pid AND a.encounter = f.encounter AND a.deleted IS NULL) AS adjustments FROM form_encounter AS f JOIN patient_data AS p ON p.pid = f.pid LEFT OUTER JOIN users AS u ON u.id = p.ref_providerID LEFT OUTER JOIN users AS w ON w.id = f.provider_id WHERE f.date >= ? AND f.date <= ? ORDER BY f.pid, f.encounter SELECT f.id, f.date, f.pid, CONCAT(w.lname, ', ', w.fname) AS provider_id, f.encounter, f.last_level_billed, f.last_level_closed, f.last_stmt_date, f.stmt_count, f.invoice_refno, f.in_collection, p.fname, p.mname, p.lname, p.street, p.city, p.state, p.postal_code, p.phone_home, p.ss, p.billing_note, p.pubpid, p.DOB, CONCAT(u.lname, ', ', u.fname) AS referrer, ( SELECT bill_date FROM billing AS b WHERE b.pid = f.pid AND b.encounter = f.encounter AND b.activity = 1 AND b.code_type != 'COPAY' LIMIT 1) AS bill_date, ( SELECT SUM(b.fee) FROM billing AS b WHERE b.pid = f.pid AND b.encounter = f.encounter AND b.activity = 1 AND b.code_type != 'COPAY' ) AS charges, ( SELECT SUM(b.fee) FROM billing AS b WHERE b.pid = f.pid AND b.encounter = f.encounter AND b.activity = 1 AND b.code_type = 'COPAY' ) AS copays, ( SELECT SUM(s.fee) FROM drug_sales AS s WHERE s.pid = f.pid AND s.encounter = f.encounter ) AS sales, ( SELECT SUM(a.pay_amount) FROM ar_activity AS a WHERE a.pid = f.pid AND a.encounter = f.encounter AND a.deleted IS NULL) AS payments, ( SELECT SUM(a.adj_amount) FROM ar_activity AS a WHERE a.pid = f.pid AND a.encounter = f.encounter AND a.deleted IS NULL) AS adjustments FROM form_encounter AS f JOIN patient_data AS p ON p.pid = f.pid LEFT OUTER JOIN users AS u ON u.id = p.ref_providerID LEFT OUTER JOIN users AS w ON w.id = f.provider_id WHERE ( 1 = 2 OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? OR f.pid = ? ) AND f.date >= ? AND f.date <= ? ORDER BY f.pid, f.encounter