добавил вывод фамилии и имени и добавил условие на отбор активных клиентов... считает ооооочень долго... можно соптимизировать как-нито?
Цитата: SELECT cr.start_card_code card, cl.sur_name, cl.name,
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_in_cards cr
ON cr.global_id = 0 AND cr.id = h.card
INNER JOIN trm_in_card_client ccl
ON ccl.card=cr.id
INNER JOIN trm_in_clients cl
ON cl.id=ccl.client
WHERE
cr.deleted = 0 AND cr.active = 1
AND cl.active=1 AND cl.deleted=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-31 23:59:59'
GROUP BY card
ORDER BY cardsum DESC
LIMIT 10;