mysql - Joins: Incorrect result when SUM() is used and only one record is displayed -


please have @ below sql query

select client_portfolio.*,  client.name "client name",  provider.name "provider name",  initial_fees.*, portfolio.vat, portfolio.invest_amount, portfolio.cash_value, sum(ongoing_fees.fee) client_portfolio  left join client on client.idclient = client_portfolio.idclient  left join portfolio on portfolio.idportfolio = client_portfolio.idportfolio  left join provider on provider.idprovider = portfolio.idprovider  left join initial_fees on initial_fees.idportfolio = portfolio.idportfolio  left join ongoing_fees on ongoing_fees.idportfolio = portfolio.idportfolio order client_portfolio.idclient  

this query generates incorrect results, if remove "sum(ongoing_fees.fee)" part, generates correct result.

ongoing_fees table, , portfolios might have ongoing_fees, while others don't. trying sum total ongoing_fees belong each portfolio seperatly , result above query. went wrong.. gave me ongoing_fees sum of entire table, , entire above query returned 1 row! how can fix this?

you using aggregate function sum() without group by give 1 single row. if want result of sum per group must use group clause @ end.

from discussion may need add

group portfolio.idportfolio before order clause.

this give sum value per idportfolio selection.

check here more on http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -