MYSQL: SQL debit / credit -


editing previous question, simplified (i hope!) problem.

let's go.

we have table in fiddle: http://sqlfiddle.com/#!9/42250/1

we have 3 different id_customer, need select made transactions in 2 or more id_shop.

in effect have data query:

select distinct(id_customer) transaction t1 exists      (select id_customer     transaction t2     t2.id_shop_where_transaction_is_done != t1.id_shop_where_transaction_is_done      , t2.id_customer = t1.id_customer) 

and data 64982 , 64984.

now need calculate credit / debit between shop(s), , having result table following:

+------------+-------+--------+ |            | debit | credit | +------------+-------+--------+ | trastevere | 5.50  | 0.00   | | monti      | 2.00  | 5.50   | | prati      | 0.00  | 2.00   | +------------+-------+--------+ 

why "trastevere" in debit of 5.50? because id_customer 64984 has charged 11.00 in trastevere , spent 5.50€ in monti.

why "monti" in credit of 5.50? because id_customer 64984 has charged 11.00 in trastevere , spent 5.50 in monti.

basically, need to

1) exclude customers spent in 1 shop (made, see query above) 2) if customer charge x in shop , spend y in shop b, need calculate , apply credit/debit.

thank much.

after discussion in comment , chat figured out solution here (with minimal table structure change) added new column (id_shop_where_money_come_from int) in table transaction hold data represent account_id money come can fallow between 2 account transfer made. here sql fiddle added column , query output desired result...

gl!


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -