sql - MySQL SUM calculation not based on number of rows but based on unique results -
i have query below:
select count(c.status) num, sum(p.total_paid_nocred) lordo, sum(p.total_paid) netto coupons c, payments p, addresses p.payment_id=c.payment_id , c.address_id=a.address_id , c.offer_id='.$off['offer_id'].' , c.address_id!=0 , a.time <= '.$off['insertion_date'].' , c.user_id!=0 , c.status in (1,2) , a.shipment_num="" , p.status=0 , p.medium!=3
the problem having when query runs can find example 5 coupons, divided between 3 different payment transactions , other 2 of them in 1 transaction.
to clear. client can buy 3 different coupons 3 different transactions, or can buy 2 coupons on 1 single transaction.
so when query sum, if single transaction has 2 linked coupons duplicates amount of transaction 2. not good, because transaction amount final is, not have duplicated more times many coupons linked transaction.
i trying use group p.payment_id
doing break rest of code working on query.
how can force sum unique transaction sum functions , leave rest is?
thanks help!
Comments
Post a Comment