Access database returns double SUM for field Income -


i have following query in access 2003 mdb.

select company.name, company.address, place.name_of_place, sum(income.value) income, sum(invoice.value) invoice  ((company left join invoice on company.companyid = invoice.companyid) left join income on company.companyid = income.companyid) inner join place on  place.postal = company.postal  group company.name, company.address, place.name_of_place, company.companyid having ((iif(isnull(sum(invoice.value)), 0, sum(invoice.value)) - iif(isnull(sum(income.value)), 0, sum(income.value))) > 0)  order company.name; 

income field value 500, query returns 1000. there must left joins income table twice searched. how solve this? i'm thinking in program simple division 2 column, i'd rather solve on database level.

regards,

vajda

when join company invoice, result have many rows rows in invoice. if company has 2 invoices, have 2 rows.

then, when join income (which not sure how many rows per company has) result 2 rows for each row of income.

you have resort sub-queries, this:

select   company.name,   company.address,   place.name_of_place,   (select sum(income.value) income income.companyid=company.companyid) income,   (select sum(invoice.value) invoice invoice.companyid=company.companyid) invoice   company inner join place on place.postal = company.postal    invoice - income > 0 order   company.name; 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -