sql - Sum a column that corresponds a aggregate from a column of another table -


sorry title, couldn't think of better subject issue.

i've 2 tables in many-to-one relationship between them , other many-to-one relatioshop in same table. need sum column corresponds aggregate column of first table.

here image illustrate want:

example have no idea how proceed this.

this should do:

select  isnull(e.idparent,e.id) id,         sum(i.value) [sum] expense_table e left join invoice_table     on i.idexpense = e.id group isnull(e.idparent,e.id) 

for updated requirement:

select  isnull(e.idparent,e.id) id,         e2.[description],         sum(i.value) [sum] expense_table e left join invoice_table     on i.idexpense = e.id inner join expense_table e2     on isnull(e.idparent,e.id) = e2.id group isnull(e.idparent,e.id),          e2.[description] 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -