select c.classifclient,r.client,r.type,sum(r.amount) _sum,sum(rd.increment) _inc from receipt_item ri
left join receipt r on r.id=ri.receipt_header and r.cash_id=ri.cash_id
left join receipt_item_discount rd on rd.receipt_item=ri.id and rd.cash_id=ri.cash_id
left join clients c on c.id=r.client
where r.date between
STR_TO_DATE('2015-10-15 00:00:00', '%Y-%m-%d %H:%i:%s')
AND STR_TO_DATE('2015-10-15 23:59:59', '%Y-%m-%d %H:%i:%s')
and ri.classif not like '.51%'
and r.client is not null
and (r.type=0 or r.type=1 or r.type=4)
and r.result=0
group by c.classifclient,r.client,r.type;
select t.classifclient,t.client,t.type,sum(t.total) _sum, t._inc
from (
select c.classifclient,r.client,r.type, ri.total, sum(rd.increment) _inc
...
group by c.classifclient,r.client,r.type,ri.cash_id,ri.id
) t
group by t.classifclient,t.client,t.type
;
select c.classifclient,r.client,r.type,sum(ri.real_amount) _sum,sum(rd.increment) _inc from receipt r -- логичней идти от чека left join receipt_item ri on r.id=ri.receipt_header and r.cash_id=ri.cash_id left join receipt_item_discount rd on rd.receipt_item=ri.id and rd.cash_id=ri.cash_id inner join clients c on c.id=r.client -- клиент в чеке может быть только один where r.date between '2015-10-15 00:00:00' AND '2015-10-15 23:59:59' -- дата-время итак в таком формате and ri.classif not like '.51%' and r.client is not null and r.type in (0,1,4) and r.result=0 group by c.classifclient,r.client,r.type;