SELECT ds.card_number card, sum(if(h.TYPE IN (1, 4, 9, 10), -d.amount, d.amount)) cardsum FROM trm_out_receipt_header h INNER JOIN trm_out_receipt_subtotal d ON h.cash_id = d.cash_id AND h.id = d.id INNER JOIN trm_out_receipt_footer f ON f.cash_id = d.cash_id AND f.id = d.id INNER JOIN trm_out_receipt_discounts ds ON ds.cash_id = h.cash_id AND ds.receipt_header = h.id WHERE ds.card_number IS NOT NULL AND ds.amount<0 AND f.RESULT = 0 AND h.TYPE IN (0, 5, 1, 4, 8, 9, 10) AND f.DATE BETWEEN '2013-10-01 00:00:00' AND '2013-10-01 23:59:59' GROUP BY ds.card_number ORDER BY cardsum DESC LIMIT 10;
SELECT ds.card_number card, sum(if(h.TYPE IN (1, 4, 9, 10), -d.amount, d.amount)) cardsum FROM trm_out_receipt_header h INNER JOIN trm_out_receipt_subtotal d ON h.cash_id = d.cash_id AND h.id = d.id INNER JOIN trm_out_receipt_footer f ON f.cash_id = d.cash_id AND f.id = d.id INNER JOIN trm_out_receipt_discounts ds ON ds.cash_id = h.cash_id AND ds.receipt_header = h.id WHERE ds.card_number IS NOT NULL AND ds.amount<0 AND f.RESULT = 0 AND h.TYPE IN (0, 5, 1, 4, 8, 9, 10) AND f.DATE BETWEEN '2013-10-01 00:00:00' AND '2013-10-01 23:59:59' GROUP BY ds.card_number ORDER BY cardsum DESC LIMIT 10;
SELECT ds.card_number card, sum(if(h.TYPE IN (1, 4, 9, 10), -d.amount, d.amount)) cardsum FROM trm_out_receipt_header h INNER JOIN trm_out_receipt_subtotal d ON h.cash_id = d.cash_id AND h.id = d.id INNER JOIN trm_out_receipt_footer f ON f.cash_id = d.cash_id AND f.id = d.id INNER JOIN trm_out_receipt_discounts ds ON ds.cash_id = h.cash_id AND ds.receipt_header = h.id WHERE ds.deleted = 0 AND ds.card_number IS NOT NULL AND f.RESULT = 0 AND h.TYPE IN (0, 5, 1, 4, 8, 9, 10) AND f.DATE BETWEEN '2013-10-01 00:00:00' AND '2013-10-01 23:59:59' GROUP BY ds.card_number ORDER BY cardsum DESC LIMIT 10;