1 2. Calculation for 1 or more visible Deposits to next non-visible Deposit or no non-visible Deposit If no hidden, loop for visible deposits (#2)) ($v) with last one to end (#1 logic, except use $v variable); else; loop for visible deposits to the next hidden deposit (#2). foreach ( my $v in @visible_deposits ) { SELECT SUM(IF(payment_type="check", amount, 0.00)) AS "Check", SUM(IF(payment_type="credit", amount, 0.00)) AS "Credit", SUM(IF(payment_type="cash", amount, 0.00)) AS "Cash" FROM transaction_log WHERE paid=1 AND transaction_id < $v AND transaction_id > (SELECT transaction_id FROM transaction_log WHERE transaction_type="Deposit" ORDER BY transaction_id LIMIT 1); push @sum, answer; } */ ?>