+ SELECT SUM( ac.amount *
+ (SELECT SUM(acc.amount) * -1
+ FROM acc_trans acc
+ INNER JOIN chart c ON (acc.chart_id = c.id AND c.link LIKE '%AR_paid%')
+ WHERE 1=1 $inwhere AND acc.trans_id = ac.trans_id)
+ / (SELECT amount FROM ar WHERE id = ac.trans_id)
+ ) AS amount, c.pos_eur
+ FROM acc_trans ac
+ LEFT JOIN chart c ON (c.id = ac.chart_id)
+ LEFT JOIN ar ON (ar.id = ac.trans_id)
+ LEFT JOIN taxkeys tk ON (tk.id = (
+ SELECT id FROM taxkeys
+ WHERE chart_id = ac.chart_id
+ AND startdate <= COALESCE(ar.deliverydate,ar.transdate)
+ ORDER BY startdate DESC LIMIT 1
+ )
+ )
+ WHERE ac.trans_id IN (SELECT DISTINCT trans_id FROM acc_trans WHERE 1=1 $subwhere)
+
+ GROUP BY c.pos_eur
+
+/*