SELECT l.name location, pm.name payment_method, SUM(p.amount) amount
FROM payment p
LEFT JOIN payment_method pm ON pm.id=p.payment_method_id
INNER JOIN invoice i ON (i.id=p.tran_id AND p.tran_type="I")
LEFT JOIN invoice_auction ia ON (ia.invoice_id = i.id)
LEFT JOIN auction a ON (a.id=ia.auction_id)
LEFT JOIN location l ON (l.id=a.location_id)
WHERE p.tran_id IN
(SELECT tran_id FROM (SELECT tran_id, MAX(created_on) last_payment FROM payment WHERE tran_type='I' AND created_on {DateRange} GROUP BY tran_id) temp)
AND i.invoice_status_id IN (3,4) AND i.account_id=19
AND p.tran_type="I"
GROUP BY l.name, pm.name;
Comments