$sql ="select cus_id,cus_name,surename,h_address,m_address,soi,street,address,address1,address2,ser_date,sum(money) money
,sum(pay) pay,sum(price_sale) price, sum(price_sale) + sum(money) amount,
sum(money) + sum(price_sale)-sum(pay) remain_pay
from (
select a.ser_date,a.cus_id,b.cus_name,b.surename,b.h_address,b.m_address,b.soi,b.street,
b.address,b.address1,b.address2
,sum(a.money) money, sum(a.pay) pay ,0 price_sale
from service a,customer b
where a.cus_id = b.cus_id
group by a.cus_id,b.cus_name,b.surename,b.h_address,b.m_address,b.soi,b.street,b.address,b.address1,b.address2
union all
select a.ser_date,a.cus_id,b.cus_name,b.surename,b.h_address,b.m_address,b.soi,b.street,
b.address,b.address1,b.address2
,0 money, 0 pay ,sum(price_sale) price_sale
from service a,customer b,sn c
where a.cus_id = b.cus_id
and a.cus_id = c.cus_id
and a.ser_id = c.ser_id
group by a.ser_date,a.cus_id,b.cus_name,b.surename,b.h_address,b.m_address,b.soi,b.street,
b.address,b.address1,b.address2
) aa
GROUP by cus_id,cus_name,surename,h_address,m_address,soi,street,address,address1,address2,ser_date
having sum(money) + sum(price_sale)-sum(pay) > 0
order by cus_id";