select * from 
	(select 
	  dealer_id,
		dealer_group_id, 
		cutoff_date,
		receiving_date,
		payment_id,
		encachement_date,
		receivable_total_price,
		receivable_total_tax,
		receivable_total_amount,
		receivable_total_carry,
		previous_charge_amount,
		previous_charge_amount + receivable_total_price + receivable_total_tax -receivable_total_amount + receivable_total_carry as total
	from receivables ) as c left join 
	(select 
	  customer_id,
	  customer_group_id,
	  cutoff_date,
	  payment_date,
	  payment_type_code,
	  encachement_date,
	  total_price,
	  total_duty,
	  total_payment,
	  total_carry,
	  pre_total,
	  total
	from sanix_production.receivables) as f on c.dealer_id=f.customer_id and c.dealer_group_id = f.customer_group_id and c.cutoff_date=f.cutoff_date
INTO OUTFILE "/tmp/receivable_match.csv" FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"';

