select i.invoice_no,so.sale_order_no,i.total_amount,so.total_amount,i.customer_name,so.customer_name from
0p4_invoice i left join 0p4_sale_order so on
so.customer_id=i.customer_id and so.total_amount=i.total_amount


select i.invoice_id,so.sale_order_id,i.invoice_no,so.sale_order_no,i.total_amount as 'inv_total',so.total_amount as 'so_total' from
0p4_invoice i left join 0p4_sale_order so on
so.customer_id=i.customer_id and so.total_amount=i.total_amount and so.date=i.date


select i.invoice_id,i.invoice_no,i.customer_id, count(id.id) as count, sum(id.quantity) as qty , i.total_amount
from 0p4_invoice i left join 0p4_invoice_items id on id.invoice_id=i.invoice_id
group by i.invoice_id



select s.sale_order_id,s.sale_order_no,s.customer_id, count(sod.sale_order_detail_id) as count, sum(sod.quantity) as qty, s.total_amount
from 0p4_sale_order s left join 0p4_sale_order_detail sod on sod.sale_order_id=s.sale_order_id
group by s.sale_order_id


select x.invoice_id, y.sale_order_id, x.invoice_no,y.sale_order_no
from (select i.invoice_id,i.invoice_no,i.customer_id, count(id.id) as count, sum(id.quantity) as qty , i.total_amount
from 0p4_invoice i left join 0p4_invoice_items id on id.invoice_id=i.invoice_id
group by i.invoice_id) x
left join
(select s.sale_order_id,s.sale_order_no,s.customer_id, count(sod.sale_order_detail_id) as count, sum(sod.quantity) as qty , s.total_amount
from 0p4_sale_order s left join 0p4_sale_order_detail sod on sod.sale_order_id=s.sale_order_id
group by s.sale_order_id) y on x.customer_id=y.customer_id and x.total_amount=y.total_amount and x.count=y.count and x.qty=y.qty
